Database and Collection Operations:
1. Create a Database:
To create a database, use the use command followed by the name of the database. For example:
use mydatabase
2. Create a Collection:
Collections are created automatically when you insert data into them. You can also
explicitly create a collection using the db.createCollection() method. For example:
db.createCollection("mycollection")
3. Insert Documents:
To insert documents into a collection, use the insertOne() or insertMany() methods.
For example:
db.mycollection.insertOne({ name: "John", age: 30 })
4. Query Documents:
Use the find() method to query documents in a collection. For example:
db.mycollection.find()
5. Update Documents:
To update documents, use the updateOne() or updateMany() methods. For
example:
db.mycollection.updateOne({ name: "John" }, { $set: { age: 35 } })
6. Delete Documents:
To delete documents, use the deleteOne() or deleteMany() methods. For example:
db.mycollection.deleteOne({ name: "John" })
7. Drop a Collection:
To drop (delete) a collection, use the drop() method. For example:
db.mycollection.drop()
8. Drop a Database:
To drop (delete) a database, use the dropDatabase() method. Be cautious, as this
operation deletes the entire database and all its collections. For example:
db.dropDatabase()