Query
Find
The find()
method is used to query a collection for documents that match specific query criteria.
The find()
returns a find action and you can call the run()
method to execute the query.
A cursor is returned that can be used to iterate over the results.
When called with an empty document, the find()
method returns a cursor pointing to the results.
You can manipulate the cursor to retrieve the data from the database.
#[derive(Debug, Serialize, Deserialize)]
struct Book {
title: String,
author: String,
}
let db = Database::open_memory()?;
let collection = db.collection::<Book>("books");
let docs = vec![
Book { title: "1984".to_string(), author: "George Orwell".to_string() },
Book { title: "Animal Farm".to_string(), author: "George Orwell".to_string() },
Book { title: "The Great Gatsby".to_string(), author: "F. Scott Fitzgerald".to_string() },
];
collection.insert_many(docs)?;
let books = collection.find(doc! {}).run()?;
for book in books {
println!("name: {:?}", book);
}
Example:
If we want to find a book written by George Orwell, we can find using the author's name like this:
let books = collection.find(doc! {
"author": "George Orwell",
}).run()?;
for book in books {
println!("name: {:?}", book);
}
If you only want to find the first result match the filter, use find_one
:
let book = collection.find_one(doc! {
"author": "George Orwell",
})?;
println!("name: {:?}", book);
Specify AND
Conditions
A compond query can specify conditions for more than one field in the collection's documents.
Implicit AND
conjunction of the query clauses.
collection.find(doc! {
"author": "George Orwell",
"title": "1984",
}).run()?;
Pagination
We can call skip()
and limit()
to paginate the results.
let books = collection
.find(doc! {})
.skip(1)
.limit(1)
.run()?;
for book in books {
println!("name: {:?}", book);
}
Sort the results
You can sort the results by calling the sort()
method.
let books = collection
.find(doc! {})
.sort(doc! {
"title": 1,
})
.run()?;
Specify OR
Conditions
Using the $or
operator, you can specify a compound query that joins each clause with a logical OR
conjunction
so that the query selects the documents in the collection that match at least one condition.
let result = typed_collection
.find(doc! {
"$or": [
{
"title": "The Grapes of Wrath",
},
{
"title": "To Kill a Mockingbird",
}
]
})
.run()?
.collect::<Result<Vec<Document>>>()?;
Advanced conditions
Name | Description |
---|---|
$eq | Matches values that are equal to a specified value. |
$gt | Matches values that are greater than a specified value. |
$gte | Matches values that are greater than or equal to a specified value. |
$in | Matches any of the values specified in an array. |
$lt | Matches values that are less than a specified value. |
$lte | Matches values that are less than or equal to a specified value. |
$ne | Matches all values that are not equal to a specified value. |
$nin | Matches none of the values specified in an array. |
$regex | Matches values that are equal to the given regular expression. |
Specify EQ
and NE
Condition
collection.find(doc! {
"age": { "$eq": 18 },
}).run()?;
The above query is specifying the age
field must be equal to 18.
collection.find(doc! {
"age": { "$ne": 18 },
}).run()?;
While this one should is specifying the age
field should not be equal to 18.
Specify GT
and GTE
Conditions
collection.find(doc! {
"age": { "$gt": 18 },
}).run()?;
The above query is specifying that the age
field must be greater than 18.
collection.find(doc! {
"age": { "$gte": 18 },
}).run()?;
While this one is specifying the age
field must be greater or equal to 18
Specify IN
and NIN
Conditions
collection.find(doc! {
"age": { "$in": [18, 19, 20] },
}).run()?;
The above query is specifying the age
field must in the provided int slice
.
collection.find(doc! {
"age": { "$nin": [18, 19, 20] },
}).run()?;
While this one is specifying the age
field must not be equal to one of the values inside the int slice
.
Specify LT
and LTE
Conditions
collection.find(doc! {
"age": { "$lt": 18 },
}).run()?;
The above query is specifying that the age
field must be less than 18.
collection.find(doc! {
"age": { "$lte": 18 },
}).run()?;
While this one is specifying the age
field must be less or equal to 18
Specify REGEX
Condition
collection.find(doc! {
"title": { "regex": polodb_core::bson::Regex {
"pattern": "\w*Ring\w*",
"options": "i",
}}
}).run()?;
The above query is specifying that the title
field should match the given regular expression with its options.