A database may have many collections. To create a collection, we can use create_collection of Database
let db = Database::open_memory().unwrap();
db.create_collection("name_of_the_collection").unwrap();
where name_of_the_collection
is the name (of the created collection) that is used for accessing the collection later.
The other way to create a collection is by using collection of Database.
let db = Database::open_memory().unwrap();
let collection = db.collection("name_of_the_collection");
Yet, this method does not actually create a collection. It only creates a collection after a document is inserted.
Sometimes, we need to specify explicitly a type of Collection. In this case, we can use Collection<Document>, which is also the default type of Collection.
use polodb_core::{bson::Document, Collection, Database};
fn main() {
let db = Database::open_memory().unwrap();
let _collection: Collection<Document> = db.collection("name_of_the_collection");
}
➡️ Next: Obtaining Collections
📘 Back: Table of contents