- Introduction to MongoDB >
- Getting Started
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.
- 0. Switch Database
- 1. Populate a collection (Insert)
- 2. Select All Documents
- 3. Specify Equality Matches
- 4. Specify Fields to Return (Projection)
Within the shell, db
refers to
your current database. Type db
to display the current
database.
The operation should return test
, which is the default
database.
To switch databases, type use <db>
. For example, to switch
to the examples
database:
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.
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.
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.
To format the results, append the .pretty()
to the
find
operation:
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"
:In the shell, copy and paste the following to return document where
qty
field equals0
:In the shell, copy and paste the following to return document where
qty
field equals0
andstatus
field equals"D"
:In the shell, copy and paste the following to return document where the
uom
field, nested inside the size document, equals"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" }
: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: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:
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:
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:
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 |
Additional Topics¶
Introduction | Developers | Administrators | Reference |
---|---|---|---|