Advanced Document Store Querying
Document ID Creation
When a new document is created, the document store automatically assigns a BSON ObjectId to the newly created document. This unique id is assigned to _id
on the returned document. This means that you can filter documents based on their id by writing a query that looks for a specific _id
. You can also create a unique id yourself before saving the document with the following code:
from bson.objectid import ObjectId
_id = ObjectId()
document_to_save = { '_id': _id, 'text': 'test document' }
Subdocuments
Please note that nested documents do not get an id assigned to them automatically. You can however, do this manually.
If you are receiving an _id
as a str
you will first need to parse the _id
to the correct type before you can use it for filtering. See the following example:
from ixoncdkingress.function.context import FunctionContext
from bson.objectid import ObjectId
@FunctionContext.expose
def remove(context: FunctionContext, note_id: str):
if context.document_db_client:
self.document_client.delete_one({'_id': ObjectId(note_id)})
If the _id
is not of an ObjectId
type the document store will not recognize it and will not filter the documents correctly.
Query Operators
When writing queries for the document store you can make use of quite a couple of advanced query operators. This page contains all the different operators that we support.
Select Operators
This paragraph contains all of the operators that you can use to find and filter documents and subdocuments.
Name | Description |
---|---|
$and | Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. |
$not | Inverts the effect of a query expression and returns documents that do not match the query expression. |
$nor | Joins query clauses with a logical NOR returns all documents that fail to match both clauses. |
$or | Joins query clauses with a logical OR returns all documents that match the conditions of either clause. |
$exists | Matches documents that have the specified field. |
$type | Selects documents if a field is of the specified type. |
$all | Matches arrays that contain all elements specified in the query. |
$elemMatch | Selects documents if element in the array field matches all the specified $elemMatch conditions. |
$size | Selects documents if the array field is a specified size. |
Update Operators
This paragraph contains all of the operators that you can use to update existing documents and subdocuments.
Field Operators
These operators can be used to update specific fields of documents.
Name | Description |
---|---|
$currentDate | Sets the value of a field to current date, either as a Date or a Timestamp. |
$inc | Increments the value of the field by the specified amount. |
$min | Only updates the field if the specified value is less than the existing field value. |
$max | Only updates the field if the specified value is greater than the existing field value. |
$mul | Multiplies the value of the field by the specified amount. |
$rename | Renames a field. |
$set | Sets the value of a field in a document. |
$setOnInsert | Sets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents. |
$unset | Removes the specified field from a document. |
Array Operators
These operators can be used to filter, find and update arrays inside individual documents.
Name | Description |
---|---|
$ | Acts as a placeholder to update the first element that matches the query condition. |
$[] | Acts as a placeholder to update all elements in an array for the documents that match the query condition. |
$[] | Acts as a placeholder to update all elements that match the arrayFilters condition for the documents that match the query condition. |
$addToSet | Adds elements to an array only if they do not already exist in the set. |
$pop | Removes the first or last item of an array. |
$pull | Removes all array elements that match a specified query. |
$push | Adds an item to an array. |
$pullAll | Removes all matching values from an array. |
Modifier Operators
These operators can be used to modify the function of other operators.
Name | Description |
---|---|
$each | Modifies the $push and $addToSet operators to append multiple items for array updates. |
$position | Modifies the $push operator to specify the position in the array to add elements. |
$slice | Modifies the $push operator to limit the size of updated arrays. |
$sort | Modifies the $push operator to reorder documents stored in an array. |
Updated 12 months ago