1. Insert Methods
db.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
2. Query Documents
db.inventory.insertMany([
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
2.1 Select All Documents in a Collection
db.inventory.find( {} )
2.2 Specify Equality Condition
{ <field1>: <value1>, ... }
db.inventory.find( { status: "D" } )
2.3 Specify Conditions Using Query Operators
{ <field1>: { <operator1>: <value1> }, ... }
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
2.4 Specify AND Conditions
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
2.5 Specify OR Conditions
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
2.6 Specify AND
as well as OR
Conditions
db.inventory.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )
3. Query on Embedded/Nested Documents
db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
3.1 Match an Embedded/Nested Document
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
# ERROR: Equality matches on the whole embedded document require an exact match of the specified <value> document,
db.inventory.find( { size: { w: 21, h: 14, uom: "cm" } } )
3.2 Query on Nested Field
db.inventory.find( { "size.uom": "in" } )
3.3 Specify Match using Query Operator
{ <field1>: { <operator1>: <value1> }, ... }
db.inventory.find( { "size.h": { $lt: 15 } } )
3.4 Specify AND
Condition
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )
4. Query an Array
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
{ item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
{ item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
{ item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
{ item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);
4.1 Match an Array
# an array with exactly two elements, "red" and "blank", in the specified order
db.inventory.find( { tags: ["red", "blank"] } )
# find an array that contains both the elements "red" and "blank"
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
4.2 Query an Array for an Element
# contains one element
db.inventory.find( { tags: "red" } )
db.inventory.find( { dim_cm: { $gt: 25 } } )
4.3 Specify Multiple Conditions for Array Elements
-
Query an Array with Compound Filter Conditions on the Array Elements
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
-
Query for an Array Element that Meets Multiple Criteria
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
-
Query for an Element by the Array Index Position
# When querying using dot notation, the field and nested field must be inside quotation marks.
db.inventory.find( { "dim_cm.1": { $gt: 25 } } )
-
Query an Array by Array Length
db.inventory.find( { "tags": { $size: 3 } } )
5. Query an Array of Embedded Documents
db.inventory.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
5.1 Query for a Document Nested in an Array
# matches the specified document
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
5.2 Specify a Query Condition on a Field in an Array of Documents
-
Specify a Query Condition on a Field Embedded in an Array of Documents
db.inventory.find( { 'instock.qty': { $lte: 20 } } )
-
Use the Array Index to Query for a Field in the Embedded Document
# When querying using dot notation, the field and index must be inside quotation marks.
db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )
5.3 Specify Multiple Conditions for Array of Documents
-
A Single Nested Document Meets Multiple Query Conditions on Nested Fields
db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )
db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )
-
Combination of Elements Satisfies the Criteria
db.inventory.find( { "instock.qty": { $gt: 10, $lte: 20 } } )
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )
6. Project Fields to Return from Query
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 All Fields in Matching Documents
# If you do not specify a projection document, the db.collection.find() method returns all fields in the matching documents.
db.inventory.find( { status: "A" } )
-
Return the Specified Fields and the _id
Field Only
# by default, the _id fields return in the matching documents.
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
-
Suppress _id
Field
# remove the _id field from the results by setting it to 0 in the projection
# With the exception of the _id field, you cannot combine inclusion and exclusion statements in projection documents.
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
-
Return All But the Excluded Fields
db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )
-
Return Specific Fields in Embedded Documents
db.inventory.find(
{ status: "A" },
{ item: 1, status: 1, "size.uom": 1 }
)
-
Suppress Specific Fields in Embedded Documents
db.inventory.find(
{ status: "A" },
{ "size.uom": 0 }
)
-
Projection on Embedded Documents in an Array
db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )
-
Project Specific Array Elements in the Returned Array
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
7. Query for Null or Missing Fields
db.inventory.insertMany([
{ _id: 1, item: null },
{ _id: 2 }
])
-
Equality Filter
# either contain the item field whose value is null or that do not contain the item field
db.inventory.find( { item: null } )
-
Type Check
# The query returns only the document where the item field has a value of null
db.inventory.find( { item : { $type: 10 } } )
-
Existence Check
# not contain the item field
db.inventory.find( { item : { $exists: false } } )
8. Update Documents
db.inventory.insertMany( [
{ item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
{ item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
{ item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
] );
-
Update Documents in a Collection
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}
-
Update a Single Document
db.inventory.updateOne(
{ item: "paper" },
{
$set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true }
}
)
-
Update Multiple Documents
db.inventory.updateMany(
{ "qty": { $lt: 50 } },
{
$set: { "size.uom": "in", status: "P" },
$currentDate: { lastModified: true }
}
)
-
Replace a Document
db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)
9. Delete Documents
db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
] );
-
Delete All Documents
db.inventory.deleteMany({})
-
Delete All Documents that Match a Condition
db.inventory.deleteMany({ status : "A" })
-
Delete Only One Document that Matches a Condition
db.inventory.deleteOne( { status: "D" } )