
Types of MongoDB Queries at a Glance.
Run the following one by one in the terminal of the folder where databases is stored.
- mongo
- Opens mongo shell
- show dbs
- displays all databases
- use <database name>
- used to get a database active
- db.<collection>.find().pretty()
- show cursor of all data from a collection
- db.<collection>.find({<field>: <integer>}).pretty()
- show cursor of data from a collection where <field> is <integer> value
- db.<collection>.find({<field>: {$gt: <integer>}}).pretty()
- show cursor of data from a collection where <field> is greater than <integer> value
- db.<collection>.find({<field>: {$gte: <integer>}}).pretty()
- show cursor of data from a collection where <field> is greater than or equal to <integer> value
- db.<collection>.findOne({<field>: <integer>})
- show first document with <field> equal to <integer> value mentioned
- db.<collection>.find({<field>: {$ne: <integer>}}).pretty()
- show cursor of data from a collection where <field> is not equal to <integer> value
- db.<collection>.find({<field>: {$lt: <integer>}}).pretty()
- show cursor of data from a collection where <field> is less than <integer> value
- db.<collection>.find({<field>: {$lte: <integer>}}).pretty()
- show cursor of data from a collection where <field> is less than or equal to <integer> value
- db.<collection>.find({"<field>.<embeded field>": {$gt: <integer>}}).pretty()
- show cursor of data where <embeded field> of <field> is greater than <integer> value from a collection
- db.<collection>.find({<array field>: <value>}).pretty()
- show cursor of data from a collection where <array field> has <value> in it
- db.<collection>.find({<array field>: [<value>]}).pretty()
- show cursor of data from a collection where <array field> has exactly [<value>] in it
- db.<collection>.find({<field>: {$in: [<value1>, <value2>]}}).pretty()
- show cursor of data from a collection where <field> either has value <value1> or <value2>
- db.<collection>.find({<field>: {$nin: [<value1>, <value2>]}}).pretty()
- show cursor of data from a collection where <field> has value anything but <value1> or <value2>
- db.<collection>.find({<field>: {$lt: <value>}}).count()
- show count of documents from a collection where <field> has value less than <value>
- db.<collection>.find({$or: [{<field1>: {$lt: <value1>}}, {<field1>: {$gt: <value2>}, {<field2>: {$gt: <value3>}]}).pretty()
- show cursor of documents from a collection where <field1> has value less than <value1> or <field1> has value less than <value2> or <field2> has value greater than <value3>
- db.<collection>.find({$nor: [{<field1>: {$lt: <value1>}}, {<field1>: {$gt: <value2>}, {<field2>: {$gt: <value3>}]}).pretty()
- show cursor of documents from a collection where <field1> does not have value less than <value1> or <field1> does not have value less than <value2> or <field2> does not have value greater than <value3>
- It is the opposite of or
- If we take count of the two queries above, if the first one returns x, the second one will return (count of all documents – x)
- db.<collection>.find({$and: [{<field1>: {$lt: <value1>}}, {<field2>: {$lt: <value2>}]}).pretty()
- show cursor of documents from a collection where <field1> has value less than <value1> and <field2> has value less than <value2>
- db.<collection>.find({<field1>: {$lt: <value1>}, <field2>: {$lt: <value2>}}).pretty()
- The previous query can be written in this form too
- db.<collection>.find({$and: [{<field1>: <value1>}, {<field1>: <value2>}]}).pretty()
- This is an example where using 4and is a must
- db.<collection>.find({<field>: {$not: {$eq: 60}}}).count()
- usage of $not
- db.<collection>.find({<field>: {$ne: 60}}).count()
- The previous query can also be written with $ne
- db.<collection>.find({<field>: {$exists: true}}).pretty()
- Returns all documents where <filed> is defined
- db.<collection>.find({<field>: {$exists: false}}).pretty()
- Returns all documents where <field> is not defined
- db.<collection>.find({<field>: {$exists: true, $ne: null}}).pretty()
- $exists can be combined with other comparison operators
- The above example returns cursor to documents where field exist and has a valid value
- db.<collection>.find({<field>: {$type: "number"}}).pretty()
- returns cursor to documents where fields has value of type number
- db.<collection>.find({<field>: {$type: "double"}}).pretty()
- All integers will also be shown since shell only knows doubles
- db.<collection>.find({<field>: {$type: ["double", "string"]}}).pretty()
- returns cursor to documents where field type is double or string
- db.<collection>.find({<field>: {$regex: /<key>/}}).pretty()
- When to use $elemMatch?
{ _id: 1, name: { first: 'John', last: 'Backus' }, birth: new Date('Dec 03, 1924'), death: new Date('Mar 17, 2007'), contribs: [ 'Fortran', 'ALGOL', 'Backus-Naur Form', 'FP' ], awards: [ { award: 'National Medal', year: 1975, by: 'NSF' }, { award: 'Turing Award', year: 1977, by: 'ACM' } ] }
- Problem: Find the person who has award 'Nation Medal' and must be awarded in year 1975
- Solution:
db.users.find({awards: {$elemMatch: {award:'National Medal', year:1975}}})
$elemMatch
allows you to match more than one component within the same array element.- Without
$elemMatch
mongo will look for users with National Medal in some year and some award in 1975s, but not for users with National Medal in 1975. - When to use $expr and $cond?
{ "_id": 1, "volume": 89, "target": 80 } {
"_id": 2, "volume": 200, "target": 117 } {
"_id": 3, "volume": 100, "target": 117 }
- Problem: Find documents where value of volume field is greater than value of target field
- Solution:
db.<collection>.find({$expr: {$gt: ["$volume", "$target"]}}).pretty()
- Problem: Volume greater than 190 and difference between Volume and Target to be a minimum of 10
- Solution:
db.<collection>.find({$expr: {$gt: [{$cond: {if: {$gte: ["$volume", 190]}, then: {$subtract: ["$volume", 10]}, else: "$volume"}}, "$target"]}}).pretty()
- How to import data from json file to MongoDB?
- mongoimport <file name>.json -d <databse name> -c <collection name> --drop --jsonArray
- db.<collection>.find({"<array>.<field of object>": <value>}).pretty()
- returns cursor to group of documents where in the <array>, one of the <object>'s <field> has the <value>
- db.<collection>.find({hobbies: {$size: 3}}).pretty()
- Only exact matches supported
- Find the count of documents in a collection where a field's first subfield's value is "Value1" and the field's second subfield's value is greater than or equal to Value2.
- Wrong way: db.<collection>.find({$and: [{"<field>.<subfield>": "Value1"}, {"<field>.<subfield>": {$gte: <Value2>}}]}).count()
- Output: 2
- Right way: db.<collection>.find({<field>: {$elemMatch: {<subfield1>: "<Value1>", <subfield2>: {$gte: Value2}}}}).pretty()
- Find documents in a collection where a field has n number of elements in its array
- db.<collection>.find({<field>: {$size: <n>}}).pretty()
- Find within an embedded document
- db.<collection>.find({"<field>.<subfield>": "Value"}).pretty()
- Check if one value of an array has two or more conditions
- Right way: db.<collection>.find({field: {$elemMatch: {$gt: 8, $lt: 10}}}).pretty()
- Wrong way: db.<collection>.find({field: {$and: [{$gt: 8, $lt: 10}]}}).pretty()
- Sorting Cursor - Sort entire collection with one field in ascending order and within that sort by another field in descending order
- db.<collection>.find().sort({"<field1.subfield>": 1, <field2>: -1}).pretty()
- Skip and Limit
- db.<collection>.find().skip(10).limit(20).pretty()
- Skip skips the number of documents and then fetches
- Limit limits the number of documents it fetches
- If sort is used, skip and limit always applies on sorted result
- Projection — Get result with only specified fields and not all fields
- db.<collection>.find(<filer>, <projection>)
- db.<collection>.find({}, {<field1>: 1, "<field2>.<subfield>": 1})
- Id is always included in the result
- db.<collection>.find({}, {<field1>: 1, "<field2>.<subfield>": 1, _id: 0})
- To exclude id, add _id: 0 to the query explicitly
- Filter and Projection
- db.<collection>.find({<field>: "Value1"}, {"<field>.$": 1}).pretty()
- db.<collection>.find({<field>: {$all: ["Value1", "Value2"]}}, {"<field>.$": 1}).pretty()
- db.<collection>.find({<field>: "Value1"}, {<field>: {$elemMatch: {$eq: "Value2"}}}).pretty()
- Project from and to in an array
- db.<collection>.find({"<field1>.<subfield>": {$gt: <n>}}, {<arrayfield2>: {$slice: [1,2]}, <field3>: 1}).pretty()
- This drops the element at 0 index and displays elements from index 1 to 2
Comments
Post a Comment