Getting Started
Insert Documents
db.collection.insertMany() can insert multiple documents into a collection.
The following example inserts new documents into the inventory collection:
db.inventory.insertMany([
{ item: "journal", qty: 25, status: "A", size: { h: 14, w: 21, uom: "cm" }, tags: [ "blank", "red" ] },
{ item: "notebook", qty: 50, status: "A", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank" ] },
{ item: "paper", qty: 100, status: "D", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank", "plain" ] },
{ item: "planner", qty: 75, status: "D", size: { h: 22.85, w: 30, uom: "cm" }, tags: [ "blank", "red" ] },
{ item: "postcard", qty: 45, status: "A", size: { h: 10, w: 15.25, uom: "cm" }, tags: [ "blue" ] }
]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5b56bd361fc0b7f2e167d164"),
ObjectId("5b56bd361fc0b7f2e167d165"),
ObjectId("5b56bd361fc0b7f2e167d166"),
ObjectId("5b56bd361fc0b7f2e167d167"),
ObjectId("5b56bd361fc0b7f2e167d168")
]
}
MongoDB adds the _id field with an ObjectId if _id is not present. insertMany() returns a document that includes the newly inserted documents _id field values.
Query Documents
Select All Documents
To select all documents in the collection, pass an empty document as the query filter document to the db.collection.find() method
db.inventory.find( {} )
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d164"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d165"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d166"), "item" : "paper", "qty" : 100, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] }
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d167"), "item" : "planner", "qty" : 75, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d168"), "item" : "postcard", "qty" : 45, "status" : "A", "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "tags" : [ "blue" ] }
To query for documents that match specific equality conditions, pass the find() method a query filter document with the <field>: <value> of the desired documents.
db.inventory.find( { status: "D" } )
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d166"), "item" : "paper", "qty" : 100, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] }
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d167"), "item" : "planner", "qty" : 75, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
Match an Embedded Document
Equality matches on the whole embedded document require an exact match of the specified <value> document, including the field order.
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d164"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
db.inventory.find( { size: { h: 14, uom: "cm", w: 21 } } )
>
Match a Field in an Embedded Document
db.inventory.find( { "size.uom": "in" } )
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d165"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d166"), "item" : "paper", "qty" : 100, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] }
Match an Element in an Array
db.inventory.find( { tags: "red" } )
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d164"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d165"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d166"), "item" : "paper", "qty" : 100, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] }
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d167"), "item" : "planner", "qty" : 75, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
Match an Array Exactly
db.inventory.find( { tags: ["red", "blank"] } )
{ "_id" : ObjectId("5b56bd361fc0b7f2e167d165"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }