We can use find of Collection to retrieve all documents in a collection. The method find receives a parameter for filters. Currently, we can use None for no filters. We then use a for loop to go through all the documents found.
let docs_found = collection.find(None).unwrap();
for doc in docs_found {
println!("{:#?}", doc.unwrap());
}
The complete code is provided below:
use polodb_core::{bson::doc, Database};
fn main() {
let db = Database::open_memory().unwrap();
let collection = db.collection("name_of_the_collection");
let docs = [
doc! {
"Name": "Alice",
"Age": 20,
},
doc! {
"Name": "Bob",
"Height": 180,
},
];
collection.insert_many(docs).unwrap();
let docs_found = collection.find(None).unwrap();
for doc in docs_found {
println!("{:#?}", doc.unwrap());
}
}
Output:
Document({
"Name": String(
"Alice",
),
"Age": Int32(
20,
),
"_id": ObjectId(
"66b610fb9d9613d5f5533aa5",
),
})
Document({
"Name": String(
"Bob",
),
"Height": Int32(
180,
),
"_id": ObjectId(
"66b610fb9d9613d5f5533aa6",
),
})
For each document, there is an entry _id
, which is the unique id of the document and is created automatically.
➡️ Next: By An Entry
📘 Back: Table of contents