MongoDB – Operators:
Types of operators:
1. Conditional Operators
2. Logical Query Operators
3. Element Query Operators
4. Evaluation Query Operators
5. Query Operators
6. Projection Operators
Throughout the Operators notes we will follow the collection data “students” and followed by the example are given below.
testset:PRIMARY>db.students.find() { "_id" : ObjectId("59245acd5273c93ad95b109b"), "firstname" : "Maria", "city" : "Banglore", "age" : 21 } { "_id" : ObjectId("59246d4b5273c93ad95b109c"), "firstname" : "sneha", "city" : "Delhi", "age" : 21 } { "_id" : ObjectId("59246d7b5273c93ad95b109d"), "firstname" : "jay", "city" : "toronto", "age" : 21 } { "_id" : ObjectId("59254ec6c2c4480ef9a1e5bb"), "firstname" : "harry", "city" : "texas", "age" : 24 } { "_id" : ObjectId("59254f0cc2c4480ef9a1e5bc"), "firstname" : "rita", "city" : "california", "age" : 22 } { "_id" : ObjectId("59254f2dc2c4480ef9a1e5bd"), "firstname" : "prasad", "city" : "newyork", "age" : 26 } { "_id" : ObjectId("59254f47c2c4480ef9a1e5be"), "firstname" : "devi", "city" : "ohio", "age" : 26 }
Conditional operators are:
$gt:(>)greater than
$lt:(<)less than
$gte:(>=)greater than equal to
$lte:(<=)less than equal to
$ne:(!=) Not Equal to
$in:in
$nin:Not in
Example:
db.students.find({age : {$gt : 22}}).pretty(); Please replace the operator with other operators and test the results.
Logical Query Operators: $and: Returns documents that satisfy both conditions
$not: Returns documents that do not satisfy the condition
$or: Returns documents that satisfy either any one of given conditions
$nor: Returns documents that do not satisfy both conditions
Example:
db.students.find( { $and : [{"age":{$lt : 22}}, {"firstname":"sneha"}] }) Please replace the operator with other operators and test the results.