MongoDB Query Operators Part-3

Share via:

MongoDB Query Operators Part-3


Evaluation Query Operators
MongoDB Evaluation operators are used to evaluate the structure of overall data or the individual fields in the document.
They are of six types of Evaluations operators
1. mod ($mod)
2. text ($text)
3. where ($where)
4. jsonSchema ($jsonSchema)
5. expr ($expr)
6. regex ($regex)
1.mod ($mod):
This operator is used to perform the modulus operation to get the required documents.
Syntax: db.collectionname.find({field_name :{ $mod: [divisor , remainder] }})
Example : In a collection named books get all the documents whose price is even number
db.books.find({price :{ $mod: [2 , 0] }})
Note : In the array you must give the two values otherwise we get an error
Ex: db.books.find({price :{ $mod: [9] }})
The above command will give you an error.
2.text ($text):
This operator is used to perform the text search on the fields which are having the string index or the text index.
To perform the text operator on the documents initially you have to create a string index on the field that you want.
Syntax: To create a text index field
db.collectionname.createIndex( { field_name:”text”} )
Example: In a collection named books get all the documents whose name has life.
db.books.find( {$text: {$search:”life”} })
3.where ($where):
This operator is used to perform JavaScript expressions to perform some flexible queries.
This makes you to run the JavaScript code as a part of query.
Syntax: db.books.find( {$where: {JS code} }
Example: In a collection named books get all the documents whose price is greater than 1500
db.books.find( {$where:function()
{ return this.price >1500 ;}
})
4. jsonSchema ($jsonSchema):
This operator is used to select all the documents that are matched with the specified schema.
Syntax: {$jsonSchema:{required field properties}
Example: In a collection named books get all the documents with book name field as string type and price as number type.
db.books.aggregate([ {$match:{
$jsonSchema:{ required : [“book_name”,”price”],
Properties:{ name:{bsonType:”string”}, } } } ])
5. expr ($expr):
This operator is used for the aggregation expressions on the specified fields
Syntax: db.collectionname.find( {$expr :{expression to evaluate} } )
Example: In a collection named books get all the documents whose price is less than needs
db.books.find( {$expr:{lt: [“$needs”,”$price“] } } )
This operator helps in writing the conditional statements like if-else,if-then-else type statements.
6. regex ($regex):
This operator is used for matching the given regular expressions or for pattern matching strings.
Syntax: db.collectionname.find( {field_name : {$regex:/pattern/,$options:’option’} } )
Example: In a collection named books get all the documents whose book_id like y20
db.books.find( {$book_id:{$regex : /y20$/ } } )
Bitwise Query Operators:
MongoDB Bitwise operators are used to perform the bitwise operations on the specified fields in a document.
They are of four types.
1. bitsAllClear ($bitsAllClear )
2. bitsAllSet ($bitsAllSet )
3. bitsAnyClear ($bitsAnyClear )
4. bitsAnySet ($bitsAnySet )
1. ($bitsAllClear ):
This operator is used to select all the documents where all the bit positions are zero i.e., clear in the specified field.
This type of operators will work on the numeric and bindata fields only.
Syntax: db.collectionname.find( {field_name : { $bitsAllClear: value} })
Example: In a collection named books get all the documents whose price field has bits clear at 2 and 3.
db.books.find({price :{ $bitsAllClear: [2 , 3] }})
2. ($bitsAllSet ):
This operator is used to select all the documents where all the bit positions are one i.e set in the specified field.
Syntax: db.collectionname.find( {field_name : { $bitsAllSet: value} })
Example: In a collection named books get all the documents whose price field has bits set at 1 and 4.
db.books.find({price :{ $bitsAllSet: [1 , 4] }})
3. ($bitsAnyClear ):
This operator is used to select all the documents where any of the mentioned bit positions are zero i.e clear in the specified field.
Syntax: db.collectionname.find( {field_name : { $bitsAnyClear: value} })
Example: In a collection named books get all the documents whose price field has bits clear at 2 and 3.
db.books.find({price :{ $bitsAnyClear: [2 , 3] }})
4. ($bitsAnySet ):
This operator is used to select all the documents where any of the mentioned bit positions are one i.e set in the specified field.
Syntax: db.collectionname.find( {field_name : { $bitsAnySet: [value1,value2]} })
Example: In a collection named books get all the documents whose price field has bits set at 1 and 4.
db.books.find({price :{ $bitsAnySet: [1 , 4] }})
Projection operators
MongoDB projection operators are used to shape output of the query result by specifying the required fields to include and exclude the other fields.
They are of two types.
1. projection ($projection)
2. slice ($slice)
1. ($projection):
This operator is used to limit the content of an array, project the first element in an array that is matched with the filter or specified condition on the array.
Syntax: db.collectionname.find( {array_name : condition},{“array_name.$”:1})
Example: In a collection named books get all the documents whose array fields have the value greater than 80.
db.books.find( {price: {$gt:80} },{“price.$”: 1} )
2. ($slice):
This operator is used to limit the content of an array to a specific number of elements.
Syntax: db.collectionname.find( { },{ array_field: {$slice:value} })
Example: In a collection named books get all the documents with the array field named prices get the first four prices of that array.
db.books.find( { },{ prices: {$slice:4} })

 

Author    : Teja
LinkedIn : https://www.linkedin.com/in/teja-sai-nadh-reddy-tatireddy-048882201
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTExperts is always active on social media platforms.
Facebook  : https://www.facebook.com/ktexperts/
LinkedIn    : https://www.linkedin.com/company/ktexperts/
Twitter       : https://twitter.com/ktexpertsadmin
YouTube   :  https://www.youtube.com/c/ktexperts
Instagram  : https://www.instagram.com/knowledgesharingplatform
Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment