DEV Community

Kiran Krishnan
Kiran Krishnan

Posted on • Originally published at kirandev.com

MongoDB Cheat Sheet

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 })
Enter fullscreen mode Exit fullscreen mode

2. Insert multiple documents - Ordered

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

3. Insert multiple documents - Unordered

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

4. Select all documents in a collection

db.products.find()
Enter fullscreen mode Exit fullscreen mode

5. Beautify returned collection

db.products.find().pretty()
Enter fullscreen mode Exit fullscreen mode

6. Find all documents that satisfy the specified query criteria

db.products.find({ status: 1 })
Enter fullscreen mode Exit fullscreen mode

7. Find first document that satisfies the specified query criteria

db.products.findOne({ status: 1 })
Enter fullscreen mode Exit fullscreen mode

8. Find the first document in the collection

db.products.findOne({})
Enter fullscreen mode Exit fullscreen mode

9. Returns the number of documents that match a query

db.products.count()
Enter fullscreen mode Exit fullscreen mode
db.products.find({ price: { $gt: 100 } }).count()
Enter fullscreen mode Exit fullscreen mode
db.products.countDocuments()
Enter fullscreen mode Exit fullscreen mode

10. Updates a single document based on the filter

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

11. Updates all documents that match the specified filter

db.products.updateMany({ quantity: 0 }, { $set: { status: 0 } })
Enter fullscreen mode Exit fullscreen mode

12. Replace an entire document

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

13. Removes a single document from a collection

db.products.deleteOne({ name: 'Product 1' })
Enter fullscreen mode Exit fullscreen mode

14. Removes all documents that match the filter

db.products.deleteMany({ price: { $gte: 100 } })
Enter fullscreen mode Exit fullscreen mode

15. Removes all documents in a collection

db.products.deleteMany({})
Enter fullscreen mode Exit fullscreen mode

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

db.products.findOneAndDelete({ name: 'Product 1' })
Enter fullscreen mode Exit fullscreen mode

17. Sort the documents - Ascending Order

db.products.find().sort({ price: 1 })
Enter fullscreen mode Exit fullscreen mode

18. Sort the documents - Descending Order

db.products.find().sort({ price: -1 })
Enter fullscreen mode Exit fullscreen mode

19. Limit the numbers of documents returned

db.products.find().limit(5)
Enter fullscreen mode Exit fullscreen mode

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

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

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

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

22. Add an item to an array

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

23. Remove the first item from an array

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

24. Remove the last item from an array

db.products.updateOne({ _id: 1 }, { $pop: { sizes: 1 } })
Enter fullscreen mode Exit fullscreen mode

25. Add multiple items to an array

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

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

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

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

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

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 } })
Enter fullscreen mode Exit fullscreen mode

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 } })
Enter fullscreen mode Exit fullscreen mode

30. Multiply the value of a field by a number

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

31. Rename a field

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

32. Deletes a particular field

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

I hope you found this article insightful.

Let's connect 🌎

Top comments (0)