Mnogo Shell
Mnogo Shell
Collection Commands
4. Show all collections:
show collections
5. Create collection
db.createCollection("TestCollection")
6. Drop a collection:
db.collectionName.drop()
Create:
7. Insert a single document:
db.collectionName.insertOne({ key: "value" })
8. Insert multiple documents:
db.collectionName.insertMany([{ key: "value1" }, { key: "value2" }])
Read
9. Find all documents:
db.collectionName.find()
10. Find documents with a filter:
db.collectionName.find({ key: "value" })
11. Find one document:
db.collectionName.findOne({ key: "value" })
Retrieves the first document that matches the filter.
12. Count documents:
db.collectionName.countDocuments({ key: "value" })
Counts the documents that match the filter.
Update:
13. Update a single document:
db.collectionName.updateOne({ key: "value" }, { $set: { newKey: "newValue" } })
Example: db.Students.updateOne({ _id: ObjectId('672f86e8bad00cdb2c0d4c79') }, { $set:
{ mobile: '1234567890' } })
Updates one document matching the filter.
Delete:
14. Delete a single document:
db.collectionName.deleteOne({ key: "value" })
Example: db.Students.deleteOne({ _id: ObjectId('6732fde7d9f6d9f82c171b39') })
Deletes one document matching the filter.
Utility Commands
15. Drop a database:
db.dropDatabase()
Deletes the current database.