top of page

MongoDB Cheat Sheet: from A to Z

MongoDB Cheat Sheet: from A to Z

Definitions

  • What is MongoDB?

MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need.

  • What is a Database?

A database holds a set of collections. A database typically has a separate file system directory to hold its data. Each database gets its own set of files on the file system. Generally, you associate a database with one application.

  • What is a Collection?

A collection is a group of documents. If a document is the MongoDB analog of a row in a relational database table, then a collection is the analog of a table.

  • What is a Document?

A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.

  • What is a Field?

A document has a dynamic schema. Dynamic schemas allow documents in the same collection to have different sets of fields and to have different field names for the common fields.

  • What is a Primary Key?

In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.

  • Why use NoSQL?

NoSQL databases are built to allow the insertion of data without a predefined schema. This makes NoSQL databases ideal for storing and processing unstructured data.

  • Why use MongoDB?

MongoDB is a document database, which means it stores data in JSON-like documents. We believe this is the most natural way to think about data, and is much more expressive and powerful than the traditional row/column model.

MongoDB Shell Commands

  • Show Database

show dbs 

This command will show all the databases in your MongoDB server.

  • Use Database

use <database_name> 

This command will switch to the database you want to use.

  • Show Collections

show collections 

This command will show all the collections in the database you are using.

  • Drop Database

db.dropDatabase() 

This command will drop the database you are using.

  • Create Collection

db.createCollection("<collection_name>") 

This command will create a collection in the database you are using.

  • Insert a Document

db.<collection_name>.insertOne({
    <key>: <value>,
    <key>: <value>,
    ...
})

This command will insert a document in the collection you are using.

  • Insert Multiple Documents

db.<collection_name>.insertMany([
    {
        <key>: <value>,
        <key>: <value>,
        ...
    },
    {
        <key>: <value>,
        <key>: <value>,
        ...
    },
    ...
])

This command will insert multiple documents in the collection you are using.

  • Find Documents

db.<collection_name>.find() 

This command will find all the documents in the collection you are using.

  • Find Documents with Query

db.<collection_name>.find({
    <key>: <value>
})

This command will find all the documents in the collection you are using that match the query.

  • Count Documents

db.<collection_name>.find({
    <key>: <value>
    }).count()

This command will count all the documents in the collection you are using that match the query.

  • Limit Documents

db.<collection_name>.find().limit(<number>) 

This command will limit the number of documents returned by the find command.

  • forEach()

db.<collection_name>.find().forEach(function(doc) {
    print("Key: " + doc.<key> + " Value: " + doc.<value>);
})

This command will iterate through all the documents in the collection you are using and print the key and value of each document.

  • Find One Document

db.<collection_name>.findOne({
    <key>: <value>
})

This command will find the first document in the collection you are using that matches the query.

  • Update a Document

db.<collection_name>.updateOne({
    <key>: <value>
}, {
    $set: {
        <key>: <value>
    }
})

This command will update the first document in the collection you are using that matches the query. $set is used to update the document.

  • Increment a Document

db.<collection_name>.updateOne({
    <key>: <value>
}, {
    $inc: {
        <key>: <value>
    }
})

This command will increment the value of the key in the first document in the collection you are using that matches the query. $inc is used to increment the value of the key.

  • Delete a Document

db.<collection_name>.deleteOne({
    <key>: <value>
})

This command will delete the first document in the collection you are using that matches the query.

  • Add new field to a Document

db.<collection_name>.updateOne({
    <key>: <value>
}, {
    $set: {
        <new_key>: <new_value>
    }
})

This command will add a new field to the first document in the collection you are using that matches the query.

  • Greater than

db.<collection_name>.find({
    <key>: {
        $gt: <value>
    }
})

This command will find all the documents in the collection you are using that have a key greater than the value.

  • Greater than or equal to

db.<collection_name>.find({
    <key>: {
        $gte: <value>
    }
})

This command will find all the documents in the collection you are using that have a key greater than or equal to the value.

  • Less than

db.<collection_name>.find({
    <key>: {
        $lt: <value>
    }
})

This command will find all the documents in the collection you are using that have a key less than the value.

  • Less than or equal to

db.<collection_name>.find({
    <key>: {
        $lte: <value>
    }
})

This command will find all the documents in the collection you are using that have a key less than or equal to the value.

  • Not equal to

db.<collection_name>.find({
    <key>: {
        $ne: <value>
    }
})

This command will find all the documents in the collection you are using that have a key not equal to the value.

  • And

db.<collection_name>.find({
    $and: [
        {
            <key>: <value>
        },
        {
            <key>: <value>
        }
    ]
})

This command will find all the documents in the collection you are using that match the query.

  • Or

db.<collection_name>.find({
    $or: [
        {
            <key>: <value>
        },
        {
            <key>: <value>
        }
    ]
})

This command will find all the documents in the collection you are using that match the query.

  • Sort

db.<collection_name>.find().sort({
    <key>: <value>
})

This command will sort all the documents in the collection you are using by the key.

  • Sort Descending

db.<collection_name>.find().sort({
    <key>: -1
})

This command will sort all the documents in the collection you are using by the key in descending order.

  • Drop Collection

db.<collection_name>.drop() 

This command will drop the collection you are using.

References


18 views0 comments
Stationary photo

Be the first to know

Subscribe to our newsletter to receive news and updates.

Thanks for submitting!

Follow us
bottom of page