MongoDB Query Operators Part-2
Element Query Operator
This operator is used to find the documents based on the datatypes and the fields in a document.
There are two element query operators.
1. exists ($exists)
2. type ($type)
1. exist ($exist):
This operator is used to get all the documents which have the specified field. This includes the documents whose specified field value is NULL.
This uses Boolean for checking the field is present in the document or not.
1. True i.e., contains the specified field.
2. False i.e., doesn’t contain the specified field
Syntax: db.collectionname.find({field_name:{$exists:Booleanvalue}})
Example: In a collection named books get all the documents which has a field called Info db.books.find( { Info: {$exists:true} } )
Similarly, to get all the documents which doesn’t contains the field called Info db.books.find( { Info: {$exists:false} } )
2. type ($type):
This type of operator is used to get all the documents where the specified type of field is matched with a field in a document.
If you want to get the documents having the multiple data types, then use array format.
These field types are specified BSON types, they are defined either by alias or type number.
Syntax:
db.collectionname.find( {field_name:{$type:alias or type number} })
Example: In a collection named books get all the documents which has a field Book_Name has String datatype
db.books.find( { Book_Name : {$type:”string”} } )
(OR)
db.books.find( { Book_Name : {$type:2} } )
Array Operators
This type of operators is used to query the documents with the array fields. They are of three array operators.
1. all ($all )
2. size ($size )
3. elemMatch ($elemMatch )
1. all ($all ):
This operator is used to get all the documents that are matched with all the specified fields in any array.
Syntax:
db.collectionname.find( {field_name:{$all:[array field values]} })
Example: In a collection named books get all the documents which has field name as info that contains weight and subscription
db.books.find( { Book_Name : {$all:[weight,subscription]} } )
2. size ($size ):
This operator is used to get all the documents that match the specified number of elements in that array field of a document.
Syntax:
db.collectionname.find( {field_name: {$size : value} } )
Example: In a collection named books get all the documents which have an array field called info with specified number of elements.
db.books.find( {info: {$size : 2} } )
3. elemMatch ($elemMatch ):
This operator is used to get all the documents that contains an array field with at least one element that matches all the specified values.
Syntax:
db.collectionname.find( {field_name: {$elemMatch : {condition1,condition_n}} } )
Example: In a collection named books get all the documents which have an array field called price list less than 500
db.books.find( {price list: {$elemMatch: {$lt:500} }})
Miscellaneous Operators
They are two Miscellaneous operators.
1. comment ($comment)
2. rand ($rand)
1. $comment:
This operator is used to add a comment to a query result.
When we use complex queries using the comment operator will help to understand that query easily.
These comments will be added in the log file which helps in tracing the profile.
Syntax:
db.collectionname.find( {query,$comment:”message you want to display”})
Example: In a collection named books with price greater than 90 write a comment called data from 2019
db.books.find( {price :{ $gte:500 },$comment:”data from 2019”})
2. $rand:
This operator is used to generate the random float values in between 0 to 1
Syntax:
{ $rand:{ }}
Used to work on the sample data with sample values.
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
Note: Please test scripts in Non Prod before trying in Production.