LAUNCHMongoDB 8.3 is built for the sub-100ms retrieval & zero downtime AI demands. Read blog >
AI DATAStop fighting your data layer. Get the memory & retrieval agents need to scale. Read blog >

MongoDB Examples

Try MongoDB Atlas Free

MongoDB is the document database designed to make it easy for developers to work with data in any form, and from any programming language. Whether you’re just firing up your first MongoDB Atlas cluster, or you’re a long-time veteran user, we put together our best set of useful examples to refresh your knowledge or help you get your bearings. Don’t hesitate to go over to the official documentation for more in-depth discussions of all of these topics.

Structuring Document Data

MongoDB documents are formatted in BSON (an extended Binary form of JSON) , which allows you ultimate flexibility in structuring data of all types. Below are just a few of the ways you can structure your documents.

Storing Nested Data Structures

Perhaps the most powerful feature of document databases is the ability to nest objects inside of documents. A good rule of thumb for structuring data in MongoDB is to prefer embedding data inside documents to breaking it apart into separate collections, unless you have a good reason (like needing to store unbounded lists of items, or needing to look up objects directly without retrieving a parent document).

 

 

Note that the name field is a nested object containing both given and family name components, and that the addresses field stores an array containing multiple addresses. Each address can have different fields in it, which makes it easy to store different types of data.

Using the MongoDB Shell

The MongoDB shell is a great tool for navigating, inspecting, and even manipulating document data. If you’re running MongoDB on your local machine, firing up the shell is as simple as typing mongo and hitting enter, which will connect to MongoDB at localhost on the standard port (27017). If you’re connecting to a MongoDB Atlas cluster or other remote instance, then add the connection string after the command mongo.

Here are a few quick shell examples:

List Databases

List Collections

Count Documents in a Collection

Find the First Document in a Collection

Find a Document by ID

Querying MongoDB Collections

The MongoDB Query Language (MQL) uses the same syntax as documents, making it intuitive and easy to use for even advanced querying. Let’s look at a few MongoDB query examples.

Find a Limited Number of Results

Find Users by Family name

 

Note that we enclose “name.family” in quotes, because it has a dot in the middle.

Query Documents by Numeric Ranges

Sort Results by a Field

Managing Indexes

MongoDB allows you to create indexes, even on nested fields in subdocuments, to keep queries performing well even as collections grow very large.

Create an Index

 

Unique indexes allow you to ensure that there is at most one record in the collection with a given value for that field – very useful with things like email addresses!

See Indexes on a Collection

 

Note that by default, collections always have an index on the _id field, for easy document retrieval by primary key, so any additional indexes will be listed after that.

Drop an Index

Conclusion

The ultimate flexibility of MongoDB means that this is far from a comprehensive list of everything you can do with it! For more information, head over to the MongoDB documentation, or take a course a MongoDB University.

Related Content:

 

Get started with Atlas today

Get started in seconds. Our free clusters come with 512 MB of storage so you can play around with sample data and get oriented with our platform.
Try FreeContact sales
GET STARTED WITH:
  • 125+ regions worldwide
  • Sample data sets
  • Always-on authentication
  • End-to-end encryption
  • Command line tools