Filtering Flashcards
Mongo DB:
Select records from collection where field1 equals value
db.collection.find({field1: value})
MongoDB
Select records from collection where field1 greater than value
db.collection.find({field1: {$gt: value}})
MongoDB
Select records from collection where field1 lower than value
db.collection.find({field1: {$lt: value}})
Mongo DB
Select records from collection where field1 lower than or equal to value
db.collection.find({field1: {$lte: value}})
Mongo DB
Select records from collection where field1 greater than or equal to value
db.collection.find({field1: {$gte: value}})
Mongo DB
Select records from collection using and condition
db.collection.find($and: [{condition1}, {condition2}])
Mongo DB
Select records from collection using regex
db.collection.find({field: {$regex: “pattern”}})
Mongo DB
Select records from collection which have a given size
db.collection.find({field: {$size: value}})
Mongo DB
Select records from collection which have a given field
db.collection.find({field: {$exists: true}})
Mongo DB
Select records from collection which don’t have a given field
db.collection.find({field: {$exists: false}})
Mongo DB
Select records from collection sorted by a given field ascending
db.collection.find().sort({field: 1})
Mongo DB
Select records from collection sorted by a given field descending
db.collection.find().sort({field: -1})
Mongo DB
Select records from collection limited by a given amount
db.collection.find().limit(limitCount)
Mongo DB
Select records from collection skiped by a given amount
db.collection.find().skip(skipCount)
Mongo DB
Select records from collection including specific fields only
db.collection.find({}, {field1: 1, fields2: 1})
Mongo DB
Select records from collection excluding specific fields
db.collection.find({}, {field1: 0, fields2: 0})
Mongo DB
How to filter records using nested values?
db.collection.find(“address.city”, “Mexico DF”)
Mongo DB
How to filter using conditions?
db.collection.find(subject: “math”, score: {$gt: 90})
Mongo DB
How to filter certain results of a given array from a specific document?
db.collection.find({_id: ObjectId(“your_document_id”), arrayfield: {$elementMatch: {field1: “value1”, field2: {$gte: 10}}}})
Mongo DB
How to filter certain results of a given array from a all documents?
db.collection.aggregate([{$match: {arrayField: {$elementMatch: {field1: “value1”, field2: {$gte: 20}}}}}])
Mongo DB
How to filter records from and array inside another array?
db.collection.find({“outerArray”: {$elementMatch: {“innerArray”: {$elemMatch: {field1: “value”, field2: {$gte: 10}}, field: “value”}}}})