MongoDB Operations
1. How do you create a database in MongoDB?
In MongoDB, you don’t explicitly create a database. A database is created automatically when you first store data in it.
- Switch to a database:
use myDatabase
If the database does not exist, MongoDB creates it when you add data.
-
Verify creation:
Add a collection and document, then use
show dbsto list databases. Empty databases won’t appear.
2. What is the insertOne method in MongoDB?
The insertOne method inserts a single document into a collection.
Syntax:
db.collection.insertOne(document)
Example:
db.users.insertOne({ name: "Alice", age: 25 })
- Adds a document to the
userscollection. - Automatically adds an
_idfield if not provided.
3. How do you insert multiple documents into a collection?
Use the insertMany method to add multiple documents.
Syntax:
db.collection.insertMany([document1, document2, ...])
Example:
db.users.insertMany([
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 22 }
])
- Ensures all or none of the documents are inserted, depending on the write concern.
4. Explain the find method in MongoDB.
The find method retrieves documents from a collection based on a query.
Syntax:
db.collection.find(query, projection)
- Query: Specifies the selection criteria.
- Projection: Specifies which fields to include/exclude.
Examples:
- Find all documents:
db.users.find({})
- Find documents with criteria:
db.users.find({ age: { $gt: 20 } })
- Use projection to include only the
namefield:
db.users.find({}, { name: 1, _id: 0 })
5. What is the difference between findOne and find?
-
findOne: Returns the first document that matches the query. -
find: Returns a cursor to all matching documents.
Examples:
-
findOne:
db.users.findOne({ age: { $gt: 20 } })
- Returns the first document where age > 20.
-
find:
db.users.find({ age: { $gt: 20 } })
- Returns all documents where age > 20.
6. How do you update a document in MongoDB?
Use the updateOne or updateMany methods.
Syntax:
db.collection.updateOne(filter, update, options)
db.collection.updateMany(filter, update, options)
Example:
Update the age of a user named "Alice":
db.users.updateOne({ name: "Alice" }, { $set: { age: 26 } })
7. What is the difference between updateOne and updateMany?
-
updateOne: Updates the first document that matches the filter criteria. -
updateMany: Updates all documents that match the filter criteria.
Examples:
-
updateOne:
db.users.updateOne({ age: { $gt: 20 } }, { $set: { verified: true } })
-
updateMany:
db.users.updateMany({ age: { $gt: 20 } }, { $set: { verified: true } })
8. How do you delete documents from a collection?
Use deleteOne or deleteMany to remove documents.
Syntax:
db.collection.deleteOne(filter)
db.collection.deleteMany(filter)
Example:
Delete all users with age > 30:
db.users.deleteMany({ age: { $gt: 30 } })
9. Explain the deleteOne and deleteMany methods.
-
deleteOne: Removes the first document that matches the filter criteria. -
deleteMany: Removes all matching documents.
Examples:
-
deleteOne:
db.users.deleteOne({ name: "Alice" })
-
deleteMany:
db.users.deleteMany({ age: { $gt: 25 } })
10. What is the drop method used for?
The drop method is used to delete an entire collection or database.
Collection Drop:
db.collection.drop()
- Deletes all documents and the collection itself.
Database Drop:
db.dropDatabase()
- Deletes the database, including all its collections.
Use with caution, as dropped data cannot be recovered.
These operations cover essential CRUD and administrative tasks in MongoDB.
Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.
Top comments (0)