Skip to content

Instantly share code, notes, and snippets.

@adafycheng
Last active December 7, 2021 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adafycheng/b67654d9260e19b65692be1b26ef3a1e to your computer and use it in GitHub Desktop.
Save adafycheng/b67654d9260e19b65692be1b26ef3a1e to your computer and use it in GitHub Desktop.
This article documents how Mongo DB CLI tool can be used to manipulate data.

MongoDB Notes

MongoDB provides several ways to manipulate data:

  • mongo / mongosh: CLI
  • Compass: GUI

This document focuses on the usage of CLI.

1. MongoDB Installation (Mac OS)

brew install mongodb-community

To start MongoDB:

brew services start mongodb/brew/mongodb-community

or, for short,

brew services start mongodb-community

To stop MongoDB:

brew services stop mongodb-community

To restart MongoDB:

brew services restart mongodb-community

2. Usage

  1. Launch CLI: MongoDB Legacy Shell or MongoDB Shell.

    • MongoDB Legacy Shell (deprecated in MongoDB v5.0):

      mongodb
    • MongoDB Shell (introduced in MongoDB v4.0):

      mongosh
  2. Display the database being used.

    db
  3. Show all database instances.

    show dbs
  4. Switch to a database instance.

    use examples
  5. To create a new database, first switch to a non-exist database and then insert a document.

    use products
    db.products.insertOne({
      name: 'Beautiful Wardrobe',
      description: 'This is a beautiful wardrobe that you shouldn\'t miss!',
      brand: 'Wonderful',
      category: 'Wardrobe',
      likes: 4,
      tags: ['wardrobe', 'bedroom'],
      size: {
        depth: '150',
        height: '180',
        width: '200',
        unit: 'cm'
      },
      date: Date()
    })
  6. Insert many documents.

    db.products.insertMany([
      {
        name: 'Beautiful Bed Frame (Double)',
        description: 'This is a beautiful wardrobe that you should never miss!',
        brand: 'Wonderful',
        category: 'Bed Frame',
        tags: ['bed frame', 'bedroom'],
        size: {
          height: '1\'',
          length: '6\'3',
          width: '4\'6'
        },
        date: Date()
      },
      {
        name: 'Wonderful Bed Frame (Single)',
        description: 'This bed frame is really wonderful!!!',
        brand: 'Wonderful',
        category: 'Bed Frame',
        tags: ['bed frame', 'bedroom'],
        size: {
          height: '1\'',
          length: '6\'3',
          width: '3\''
        },
        date: Date()
      },
      {
        name: '3-seats Sofa',
        description: 'Comfortable sofa',
        brand: 'Wonderful',
        category: 'Sofa',
        tags: ['sofa', 'living room'],
        size: {
          height: '100',
          length: '200',
          width: '60',
          unit: 'cm'
        },
        date: Date()
      }
    ])
  7. Find all documents.

    db.products.find()
  8. Find by criteria.

    db.products.find({ category: 'Wardrobe' })
    • To print pretty in legacy Mongo shell
    db.products.find({ brand: 'Wonderful' }).pretty()
  9. Find by limiting number of documents returned.

    db.products.find().limit(2)
  10. Find only one document.

    db.products.findOne({ category: 'Wardrobe' })
  11. Find by descending order.

    db.products.find().sort({ category: -1 }).limit(2)
  12. Print documents.

    db.products.find().forEach(function(doc) { print('Product Name: ' + doc.name) })
  13. Update single document, even if more than one document is found.

    db.products.update(
      { name: 'Beautiful Bed Frame (Double)' },
      {
        $set: {
          date: Date(),
          likes: 3
        }
      },
      {
        upsert: true
      }
    )
  14. Update all matched documents.

    db.products.updateMany(
      { brand: 'Wonderful' },
      {
        $set: {
            brand: 'Wonderful',
            date: Date()
        },
        $unset: {
            likes: 4
        }
      }
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment