Profile picture

Software Engineer (Open Source, Dev Tools) at BoxyHQ.

Follow me on Twitter

MongoDB Cheat Sheet

November 04, 2021

This cheat sheet is filled with some handy commands to get started with CRUD operations with MongoDB.

I have created a Github Gist to keep all these commands.

1. Insert a single document

db.products.insertOne({ name: 'Product 1', price: 200 })

2. Insert multiple documents - Ordered

db.products.insertMany([
  { name: 'Product 1', price: 200 },
  { name: 'Product 2', price: 100 }
])

3. Insert multiple documents - Unordered

db.products.insertMany(
  [
    { name: 'Product 1', price: 200 },
    { name: 'Product 2', price: 100 }
  ],
  { ordered: false }
)

4. Select all documents in a collection

db.products.find()

5. Beautify returned collection

db.products.find().pretty()

6. Find all documents that satisfy the specified query criteria

db.products.find({ status: 1 })

7. Find first document that satisfies the specified query criteria

db.products.findOne({ status: 1 })

8. Find the first document in the collection

db.products.findOne({})

9. Returns the number of documents that match a query

db.products.count()
db.products.find({ price: { $gt: 100 } }).count()
db.products.countDocuments()

10. Updates a single document based on the filter

db.products.updateOne({ name: 'Product 1' }, { $set: { price: 210 } })

11. Updates all documents that match the specified filter

db.products.updateMany({ quantity: 0 }, { $set: { status: 0 } })

12. Replace an entire document

db.products.updateOne(
  { name: 'Product 1' },
  { name: 'Product 11', price: 300, status: 0 }
)

13. Removes a single document from a collection

db.products.deleteOne({ name: 'Product 1' })

14. Removes all documents that match the filter

db.products.deleteMany({ price: { $gte: 100 } })

15. Removes all documents in a collection

db.products.deleteMany({})

16. Deletes a single document based on the filter and returning the deleted document

db.products.findOneAndDelete({ name: 'Product 1' })

17. Sort the documents - Ascending Order

db.products.find().sort({ price: 1 })

18. Sort the documents - Descending Order

db.products.find().sort({ price: -1 })

19. Limit the numbers of documents returned

db.products.find().limit(5)

20. Increments the value of the field by the specified amount

db.products.updateOne({ title: 'Product 1' }, { $inc: { quantity: 1 } })

21. Decrement the value of the field by the specified amount

db.products.updateOne({ title: 'Product 1' }, { $inc: { quantity: -1 } })

22. Add an item to an array

db.products.updateOne({ _id: 1 }, { $push: { sizes: 'small' } })

23. Remove the first item from an array

db.products.updateOne({ _id: 1 }, { $pop: { sizes: -1 } })

24. Remove the last item from an array

db.products.updateOne({ _id: 1 }, { $pop: { sizes: 1 } })

25. Add multiple items to an array

db.products.updateOne(
  { _id: 1 },
  { $push: { sizes: { $each: ['small', 'large', 'medium'] } } }
)

26. Add an item to an array unless the item is already present

db.products.updateOne({ _id: 1 }, { $addToSet: { sizes: 'large' } })

27. Sets the value of a field to the current date

db.products.updateOne({ _id: 1 }, { $currentDate: { lastModified: true } })

28. Update the field if the specified value is less than the current value of the field

db.products.updateOne({ _id: 1 }, { $min: { price: 150 } })

29. Update the field if the specified value is greater than the current value of the field

db.products.updateOne({ _id: 1 }, { $max: { price: 250 } })

30. Multiply the value of a field by a number

db.products.updateOne({ _id: 1 }, { $mul: { quantity: 2 } })

31. Rename a field

db.products.updateOne({ _id: 1 }, { $rename: { quantity: 'qty' } })

32. Deletes a particular field

db.products.updateOne({ _id: 1 }, { $unset: { quantity: '' } })

Kiran K