MongoDB Commands On Operators
To create a collection called student data.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.createCollection("studentdata") |
To get all the documents in a collection called student data without any filter.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find() |
To get all the documents whose total marks is equals to 560.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({totalmarks:{$eq:560}}) |
To get all the documents whose total marks is not equals to 560.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({totalmarks:{$ne:560}}) |
To get all the documents whose total marks is greater than 500.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({totalmarks:{$gt:500}}) |
To get all the documents whose total marks is less than 550.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({totalmarks:{$lt:550}}) |
To get all the documents whose total marks is less than or equals to 520.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({totalmarks:{$lte:520}}) |
To get all the documents whose total marks is greater than or equals to 520.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({totalmarks:{$gte:520}}) |
To get all the documents whose total marks is present in given array of values.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({totalmarks:{$in:[400,499]}}) |
To get all the documents whose total marks is not present in given array of values.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({totalmarks:{$nin:[442,499,560]}}) |
To get all the documents which satisfy both the given conditions, i.e., total marks greater than 550 and has two siblings.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({$and:[{totalmarks:{$gt:550}},{siblings:2}]}) |
To get all the documents which satisfy any one of the given conditions, i.e., total marks less than 560 or has two siblings.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({$or:[{totalmarks:{$lt:560}},{siblings:2}]}) |
To get all the documents which do not satisfy both the given conditions, i.e., total marks greater than 560 and has two siblings.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({$nor:[{totalmarks:{$gt:560}},{siblings:2}]}) |
To get all the documents which is having the total marks field value less than 500, this includes the documents which do not have the total marks field.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({totalmarks:{$not:{$gt:500}}}) |
To get all the documents which do not have the field name called “siblings.”
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({siblings:{$exists:false}}) |
To get all the documents which is having the field name called “name.”
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({name:{$exists:true}}) |
To get all the documents whose datatype of the field “siblings” is an integer.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({siblings:{$type:"int"}}) |
To get all the documents whose datatype of the field “name” is a string.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({name:{$type:"string"}}) |
To get all the documents that has a array field whose array size is exactly two.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({favsports:{$size:2}}) |
To get the total marks which are divisible by 10, first parameter in mod is the number you want to divide by, the second one is the remainder.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({totalmarks:{$mod:[10,0]}}) |
To create a text index on the name field
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.createIndex({name:"text"}) |
After creating the text index on the field, you can use it for search, in this case this want to search the name antony.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find({$text:{$search:"antony"}}) |
To validate the schema that you need and store it in the variable called myschema.
1 2 3 4 5 6 7 8 9 10 11 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> myschema={ $jsonSchema: { required: [ "name", "totalmarks" ], properties: { name: { bsonType: "string", description: "must be a string and is required" } } } } |
To get all the documents that matches with the schema mentioned above.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find(myschema) |
Another way to get all the documents that matches with the schema.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.aggregate( [ { $match: myschema } ] ) |
To get all the documents that didn’t match with the specified above schema.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find( { $nor: [ myschema ] } ) |
To get only the first two values of the array field called favsports.
1 |
Atlas atlas-dsrabt-shard-0 [primary] practisedata> db.studentdata.find( { },{favsports: {$slice:2} }) |
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.