Query Documents

mongo --host 127.0.0.1:27017

The examples on this page use the inventory collection. To populate the inventory collection, run the following:

db.inventory.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 5 }

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" }
]);
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("5b56cc3de68eb0362ce719ad"),
                ObjectId("5b56cc3de68eb0362ce719ae"),
                ObjectId("5b56cc3de68eb0362ce719af"),
                ObjectId("5b56cc3de68eb0362ce719b0"),
                ObjectId("5b56cc3de68eb0362ce719b1")
        ]
}

Select All Documents in a Collection

db.inventory.find( {} )
{ "_id" : ObjectId("5b56cc3de68eb0362ce719ad"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5b56cc3de68eb0362ce719ae"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
{ "_id" : ObjectId("5b56cc3de68eb0362ce719af"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("5b56cc3de68eb0362ce719b0"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("5b56cc3de68eb0362ce719b1"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

This operation corresponds to the following SQL statement:

SELECT * FROM inventory

Specify Equality Condition

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" ] }

This operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "D"

Specify Conditions Using Query Operators

The following example retrieves all documents from the inventory collection where status equals either "A" or "D":

db.inventory.find( { status: { $in: [ "A", "D" ] } } )
{ "_id" : ObjectId("5b56cc3de68eb0362ce719ad"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5b56cc3de68eb0362ce719ae"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
{ "_id" : ObjectId("5b56cc3de68eb0362ce719af"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("5b56cc3de68eb0362ce719b0"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("5b56cc3de68eb0362ce719b1"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status in ("A", "D")

Specify AND Conditions

The following example retrieves all documents in the inventory collection where the status equals "A" and qty is less than ($lt) 30:

db.inventory.find( { status: "A", qty: { $lt: 30 } } )
{ "_id" : ObjectId("5b56cc3de68eb0362ce719ad"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" AND qty < 30

Specify OR Conditions

The following example retrieves all documents in the collection where the status equals "A" or qty is less than ($lt) 30:

db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
{ "_id" : ObjectId("5b56cc3de68eb0362ce719ad"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5b56cc3de68eb0362ce719ae"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
{ "_id" : ObjectId("5b56cc3de68eb0362ce719b1"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" OR qty < 30

Specify AND as well as OR Conditions

In the following example, the compound query document selects all documents in the collection where the status equals "A" and either qty is less than ($lt) 30 or item starts with the character p:

db.inventory.find( {
     status: "A",
     $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )
{ "_id" : ObjectId("5b56cc3de68eb0362ce719ad"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5b56cc3de68eb0362ce719b1"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")

Additional Query Tutorials

Behavior

Cursor

The db.collection.find() method returns a cursor to the matching documents.

Read Isolation

For reads to replica sets and replica set shards, read concern allows clients to choose a level of isolation for their reads. For more information, see Read Concern.

results matching ""

    No results matching ""