Navigation

Getting Started

The following page provides various examples for querying in the MongoDB shell. For examples using MongoDB drivers, refer to the links in the Additional Examples section.

Examples

Click inside the shell to connect. Once connected, you can run the examples in the shell above.

Within the shell, db refers to your current database. Type db to display the current database.

db

The operation should return test, which is the default database.

To switch databases, type use <db>. For example, to switch to the examples database:

use examples

You do not need to create the database before you switch. MongoDB creates the database when you first store data in that database (such as create the first collection in the database).

To verify that your database is now examples, type db in the shell above.

db

To create a collection in the database, see the next tab.

MongoDB stores documents in collections. Collections are analogous to tables in relational databases. If a collection does not exist, MongoDB creates the collection when you first store data for that collection.

The following example uses the db.collection.insertMany() method to insert new documents into the inventory collection. You can copy and paste the example into the shell above.

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: 10, status: "D", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank", "plain" ] },
   { item: "planner", qty: 0, 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" ] }
]);

// MongoDB adds an _id field with an ObjectId value if the field is not present in the document

The operation returns a document that contains the acknowledgement indicator and an array that contains the _id of each successfully inserted documents.

To verify the insert, you can query the collection (See the next tab).

To select the documents from a collection, you can use the db.collection.find() method. To select all documents in the collection, pass an empty document as the query filter document to the method.

In the shell, copy and paste the following to return all documents in the inventory collection.

db.inventory.find({})

To format the results, append the .pretty() to the find operation:

db.inventory.find({}).pretty()

Note

The example assumes that you have populated the inventory collection from the previous step.

For an equality match (i.e. <field> equals <value>), specify <field>: <value> in the query filter document and pass to the db.collection.find() method.

Note

The examples assume that you have populated the inventory collection.

  • In the shell, copy and paste the following to return documents where status field equals "D":

    db.inventory.find( { status: "D" } );
    
  • In the shell, copy and paste the following to return document where qty field equals 0:

    db.inventory.find( { qty: 0 } );
    
  • In the shell, copy and paste the following to return document where qty field equals 0 and status field equals "D":

    db.inventory.find( { qty: 0, status: "D" } );
    
  • In the shell, copy and paste the following to return document where the uom field, nested inside the size document, equals "in":

    db.inventory.find( { "size.uom": "in" } )
    
  • In the shell, copy and paste the following to return document where the size field equals the document { h: 14, w: 21, uom: "cm" }:

    db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
    

    Equality matches on the embedded document require an exact match, including the field order.

  • In the shell, copy and paste the following to return documents where the tags array contains "red" as one of its elements:

    db.inventory.find( { tags: "red" } )
    

    If the tags field is a string instead of an array, then the query is just an equality match.

  • In the shell, copy and paste the following to return documents where the tags field matches the specified array exactly, including the order:

    db.inventory.find( { tags: [ "red", "blank" ] } )
    

To specify fields to return, pass a projection document to the db.collection.find(<query document>, <projection document>) method. In the projection document, specify:

  • <field>: 1 to include a field in the returned documents
  • <field>: 0 to exclude a field in the returned documents

In the shell, copy and paste the following to return the _id, item, and the status fields from all documents in the inventory collection:

db.inventory.find( { }, { item: 1, status: 1 } );

You do not have to specify the _id field to return the field. It returns by default. To exclude the field, set it to 0 in the projection document. For example, copy and paste the following to return only the item, and the status fields in the matching documents:

db.inventory.find( {}, { _id: 0, item: 1, status: 1 } );

Next Steps

Set up Your Own Deployment

To set up your own deployment:

MongoDB Atlas Free Tier Cluster MongoDB Atlas is a fast, easy, and free way to get started with MongoDB. To learn more, see the Getting Started with Atlas tutorial.
Local MongoDB installation For more information on installing MongoDB locally, see Install MongoDB.

Additional Examples

For additional examples, including MongoDB driver specific examples (Python, Java, Node.js, etc.), see:

Query document examples
Update document examples
Delete document examples