0% found this document useful (0 votes)
13 views1 page

Mnogo Shell

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

Mnogo Shell

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Basic Database Commands

1. Show all databases:


show dbs
Lists all databases on the MongoDB server.
2. Switch to a database (or create if not exists):
use databaseName for example: use UCP
3. Display current database:
db
Shows the name of the currently selected database.

Collection Commands
4. Show all collections:
show collections
5. Create collection
db.createCollection("TestCollection")
6. Drop a collection:
db.collectionName.drop()

CRUD (Create, Read, Update, Delete) Commands

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.

You might also like