Skip to content

Latest commit

 

History

History
105 lines (74 loc) · 3.19 KB

File metadata and controls

105 lines (74 loc) · 3.19 KB

MongoDB Questions (Setup & CRUD Operations)

Q1. Describe the steps involved in setting up a MongoDB instance on your local machine.

  • Download the appropriate MongoDB package for your operating system.
  • Extract the downloaded archive.
  • Set environment variables for MongoDB_HOME and PATH.
  • Start the MongoDB daemon service.
  • Connect to the MongoDB shell using the mongo command.

Q2. How do you check if MongoDB is running?

  • Use the mongod --version command to check the MongoDB version.
  • Use the ps aux | grep mongod command to see if the mongod process is running.

Q3. What are the default host and port where MongoDB server listens?

By default, MongoDB listens on localhost (127.0.0.1) and port 27017.

Q4. How do you create, read, update, and delete documents in MongoDB?

  • Create: db.collectionName.insertOne(document) or db.collectionName.insertMany(documents)
  • Read: db.collectionName.find() or db.collectionName.findOne()
  • Update: db.collectionName.updateOne() or db.collectionName.updateMany()
  • Delete: db.collectionName.deleteOne() or db.collectionName.deleteMany()

// Create
db.users.insertOne({ name: "John Doe", age: 30, city: "New York" });

db.products.insertMany([
    { name: "Product A", price: 19.99 },
    { name: "Product B", price: 29.99 },
    { name: "Product C", price: 9.99 }
]);

// Read
db.users.find().pretty(); // Find all documents

db.products.findOne({ price: { $gt: 20 } }); // Find one product with price greater than 20

// Update
db.users.updateOne({ name: "John Doe" }, { $set: { age: 31 } });

db.products.updateMany({ price: { $lt: 10 } }, { $inc: { price: 2 } });

// Delete
db.users.deleteOne({ name: "John Doe" });

db.products.deleteMany({ price: { $gt: 30 } });

Q5. Explain the difference between find() and findOne() methods.

  • find() returns an array of all documents matching the query, while
  • findOne() returns only the first matching document.

Q6. Explain the use of update operators like $set, $inc, and $push.

  • $set: Sets the value of a field to the specified value.
  • $inc: Increments a numeric field by a specified value.
  • $push: Adds one or more elements to an array.

Q7. How do you create a new database in MongoDB?

A new database is created implicitly when you switch to it and insert data. For example:

use myNewDatabase
db.myCollection.insertOne({ name: "John Doe" })

Q8. How do you replace a document in MongoDB?

Use the replaceOne method to replace a document completely. For example, to replace a document with name John Doe:

db.collectionName.replaceOne(
  { name: "John Doe" },
  { name: "John Doe", age: 31, occupation: "Manager" }
)

Q9. How do you list all collections in a MongoDB database?

Use the show collections command in the MongoDB shell:

Q10 How do you create a user with read and write permissions in MongoDB?

Use the createUser method. For example, to create a user dbUser with read and write permissions:

db.createUser({
  user: "dbUser",
  pwd: "password",
  roles: [{ role: "readWrite", db: "myDatabase" }]
})