MongoDB Query
MongoDB Query
Reference: https://docs.mongodb.com/manual/
Ascending/Descending Sort
• Specify in the sort parameter the field or fields to sort by and a value
of 1 or -1 to specify an ascending or descending sort respectively
• If, instead, you wish to find an array that contains both the elements
"red" and "blank", without regard to order or other elements in the
array, use the $all operator:
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
Query an Array for an Element
• To query if the array field contains at least one element with the specified
value, use the filter { <field>: <value> } where <value> is the element value
db.inventory.find( { tags: "red" } )
dim_cm is an array!
Specify Multiple Conditions for Array Elements
• Query an Array with Compound Filter Conditions on the Array Elements
• The following example queries for documents where the dim_cm array
contains elements that in some combination satisfy the query
conditions; e.g., one element can satisfy the greater than 15 condition
and another element can satisfy the less than 20 condition, or a single
element can satisfy both
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
• The following query matches documents where any document nested in the instock
array has the qty field greater than 10 and any document (but not necessarily the
same embedded document) in the array has the qty field less than or equal to 20:
db.inventory.find( { "instock.qty": { $gt: 10, $lte: 20 } } )
• The following example queries for documents where the instock array has at least one
embedded document that contains the field qty equal to 5 and at least one
embedded document (but not necessarily the same embedded document) that
contains the field warehouse equal to A:
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )
Specify Multiple Conditions for Array of Documents
• A Single Nested Document Meets Multiple Query Conditions on Nested Fields
• Use $elemMatch operator to specify multiple criteria on an array of embedded
documents such that at least one embedded document satisfies all the specified criteria
• The following example queries for documents where the instock array has at least one
embedded document that contains both the field qty equal to 5 and the field
warehouse equal to A (order does not matter):
db.inventory.find( {instock: { $elemMatch: { qty: 5, warehouse: "A" } } } )
• The following example queries for documents where the instock array has
at least one embedded document that contains the field qty that is greater
than 10 and less than or equal to 20:
db.inventory.find( {instock: { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )
Query for Null or Missing Fields
• Consider the following collection
db.inventory.insertMany([
{ _id: 1, item: null },
{ _id: 2 }
])
• The { item : null } query matches documents that either contain the
item field whose value is null or that do not contain the item field
db.inventory.find( { item : null } )
What will be returned?
Query for Null or Missing Fields
• The { item : { $type: 10 } } query matches only documents that contain
the item field whose value is null; i.e., the value of the item field is of
BSON Type Null (type number 10) :
db.inventory.find( { item : { $type: 10 } } )
db.inventory.insertMany( [
{ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A",
qty: 5 } ] },
{ item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C",
qty: 5 } ] },
{ item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty:
60 } ] },
{ item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse:
"A", qty: 40 } ] },
{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse:
"B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
Return the Specified Fields and the _id Field Only
• A projection can explicitly include several fields by setting the <field> to 1 in
the projection document. The following operation returns all documents that
match the query
• General syntax: db.collection.find(query, projection)
db.inventory.find( { status: "A" }, { item : 1, status : 1 } )
• You can remove the _id field from the results by setting its exclusion <field> to
0 in the projection, as in the following example:
db.inventory.find( { status: "A" }, { item : 1, status : 1, _id: 0 } ) // only _id can mix
db.inventory.find( { status: "A" }, { item : 0, status : 1 } ) // not working
Return the Specified Fields and the _id Field Only
• Similarly, you can use a projection to exclude specific fields. The following
example which returns all fields except for the status and the instock fields in
the matching documents:
db.inventory.find( { status: "A" }, { status : 0, instock : 0 } )
Project Fields to Return from Query
• Return Specific Fields in Embedded Documents
db.inventory.find( { status: "A" }, { item : 1, status : 1, "size.uom": 1 })
• Suppress
db.inventory.find( { status: "A" }, { "size.uom": 0 })