Find
CapybaraDB allows you to query and retrieve documents from a collection using operations similar to MongoDB's find()
functionality. You can use filters to match specific documents, and optionally, specify projection fields, sort order, and pagination options.
Find Operation
The find
operation is used to query multiple documents in a collection based on a filter. You can optionally include projection, sort, limit, and skip parameters for more advanced queries.
Example Python Code for find
Here’s how you can query documents using Python, by applying a filter, projection, and sort operation:
Python
# Filter to match the document(s)
filter_criteria = {
"age": {"$gt": 25}
}
# Optional projection, sort, limit, and skip parameters
projection = {
"name": 1,
"age": 1,
"_id": 0 # Exclude _id
}
sort = {
"age": -1 # Sort by age in descending order
}
limit = 5
skip = 0
# Sending the request
response = collection.find(filter_criteria, projection, sort, limit, skip)
Find Response
A successful find operation will return a JSON response containing the matching documents in the collection. Here’s an example response:
JSON
{
"documents": [
{
"name": "Alice Smith",
"age": 29
},
{
"name": "Bob Johnson",
"age": 40
}
]
}
Parameters for Find Operations
Parameter | Description |
---|---|
filter | A query object to match the documents to retrieve. This works the same way as MongoDB filters, allowing you to specify conditions to find the documents. For more details, refer to the filter operator syntax. |
projection (optional) | A field specification object to control which fields are returned in the result set. Use 1 to include and 0 to exclude fields. |
sort (optional) | An object specifying the sort order for the result set (e.g., { "age": 1 } to sort by age in ascending order, or { "age": -1 } for descending). |
limit (optional) | A number to limit the number of documents returned. |
skip (optional) | A number to skip the first n documents in the result set, useful for pagination. |