10

A-Z: MongoDB Cheat Sheet🌱

 1 year ago
source link: https://dev.to/burakboduroglu/mongodb-cheat-sheet-1a6a
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

New Post 🆕

Boost Your Programming Efficiency: Essential Tools for Success ⚙️


Contents 📖

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.


Thank You 🌿🌴🌲

Thanks for reading! I hope you found this useful. If you have any questions or feedback.
If you liked this post please give a emoji or comment. ⭐️


References

My other contents


Back to Top


Recommend

  • 127
    • zeroturnaround.com 6 years ago
    • Cache

    Java 9 modules cheat sheet zeroturnaround.com

    Java 9 Modules Cheat SheetJava Application DevelopmentJava UpdatesIn our Java 9 Modules Cheat Sheet, we go over the most useful declarations, mechanisms, attributes, and flags for the Java Platform Module...

  • 96

    Java 8 has already been with us for quite some time. Let’s reflect on it and talk about the best practices that have naturally grown in its typical usage. In this post we’ll look at the hot topics of the Java 8 language: default methods, lambdas...

  • 69
    • the-awesome-git-cheat-sheet.com 6 years ago
    • Cache

    The awesome git cheat sheet

    This Awesome Git cheat sheet serves as a quick reference for basic Git commands. It helps you learn Git. Git branches, remote repositories, undoing changes, etc.

  • 83
    • 掘金 juejin.im 6 years ago
    • Cache

    Axois 作弊表(Cheat Sheet)

    英文原文链接 GET 请求 // Make a request for a user with a given ID axios.get('/user?ID=12345') .then(function (response) { console.l

  • 67
    • www.tuicool.com 6 years ago
    • Cache

    JavaScript Regex Cheat Sheet

    Successfully working with regular expressions requires you to know what each special character, flag and method does. This is a regular expressions cheat sheet which you can refer to when trying to remember how a method,...

  • 40
    • www.tuicool.com 6 years ago
    • Cache

    A Performance Cheat Sheet for MongoDB

    Database performance affects organizational performance, and we tend to want to look for a quick fix. There are many different...

  • 60

    README.md

  • 2
    • gist.github.com 1 year ago
    • Cache

    MongoDB Cheat Sheet 2022

    MongoDB Cheat Sheet 2022 MongoDB Crash Course 2022 < TODO: Add Video Link Table of Contents Check monosh Version mongosh --version Star...

  • 5
    • gist.github.com 1 year ago
    • Cache

    MongoDB Cheat Sheet

    MongoDB Cheat Sheet Show All Databases show dbs Show Current Database db Create Or Switch Database use acme db.dropDat...

  • 4
    • gist.github.com 1 year ago
    • Cache

    MongoDB C# Driver Cheat Sheet

    MongoDB C# Driver Cheat Sheet (C) 2015 by Derek Hunziker, (C) 2017 by AppsOn As of releasing MongoDB...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK