MCQs Of Day 26: MongoDB with Node.js
1. What is MongoDB?
- A) A Relational Database
- B) A NoSQL Database
- C) A Programming Language
- D) An Operating System
Answer: B) A NoSQL Database
Explanation: MongoDB is a NoSQL database, meaning it is not based on the traditional relational model.
2. Which of the following is used to connect Node.js to MongoDB?
- A) MongoDB Driver
- B) MongoDB API
- C) MongoDB Client
- D) MongoDB Query Builder
Answer: A) MongoDB Driver
Explanation: The MongoDB Node.js driver is used to connect Node.js applications to MongoDB.
3. What is the method to insert a single document into MongoDB?
- A) insertAll()
- B) insertOne()
- C) insertMany()
- D) addDocument()
Answer: B) insertOne()
Explanation: insertOne() is used to insert a single document into MongoDB.
4. How can you retrieve all documents from a collection?
- A) findAll()
- B) find()
- C) get()
- D) query()
Answer: B) find()
Explanation: find() retrieves documents based on the query, and you can use .toArray() to convert the result into an array.
5. Which method is used to update a document in MongoDB?
- A) update()
- B) updateOne()
- C) modify()
- D) change()
Answer: B) updateOne()
Explanation: updateOne() is used to update a single document in MongoDB.
6. What operator is used to compare values greater than or equal to in MongoDB?
- A) $gte
- B) $gt
- C) $eq
- D) $lt
Answer: A) $gte
Explanation: The $gte operator is used to match values that are greater than or equal to a specified value.
7. How do you delete a document from MongoDB?
- A) delete()
- B) deleteOne()
- C) remove()
- D) removeOne()
Answer: B) deleteOne()
Explanation: deleteOne() is used to delete a single document from a collection.
8. What is a MongoDB collection equivalent to in relational databases?
- A) Table
- B) View
- C) Row
- D) Column
Answer: A) Table
Explanation: In MongoDB, a collection is analogous to a table in a relational database.
9. Which of the following MongoDB methods is used to insert multiple documents at once?
- A) insertOne()
- B) insertAll()
- C) insertMany()
- D) addMultiple()
Answer: C) insertMany()
Explanation: insertMany() is used to insert multiple documents at once into a MongoDB collection.
10. Which MongoDB method is used to close a database connection?
- A) disconnect()
- B) close()
- C) stop()
- D) exit()
Answer: B) close()
Explanation: The close() method is used to close the MongoDB connection in Node.js.
(Further MCQs can be similarly written and explained for additional topics in MongoDB such as Aggregation, Query Filters, Projection, Indexing, etc.)
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here is the continuation of the 50 Multiple Choice Questions (MCQs) with detailed explanations on MongoDB with Node.js.
11. What is the default port for MongoDB?
- A) 27017
- B) 8080
- C) 3306
- D) 5432
Answer: A) 27017
Explanation: The default port for MongoDB is 27017.
12. How do you start a MongoDB server?
- A) mongo start
- B) mongod
- C) mongo-server
- D) start-mongo
Answer: B) mongod
Explanation: To start the MongoDB server, use the mongod command.
13. Which of the following is a valid MongoDB query operator?
- A) $like
- B) $eq
- C) $contains
- D) $match
Answer: B) $eq
Explanation: $eq is used to compare values for equality in MongoDB queries.
14. Which of the following functions is used to connect to MongoDB using the MongoDB driver in Node.js?
- A) connectDB()
- B) connectToMongo()
- C) MongoClient.connect()
- D) db.connect()
Answer: C) MongoClient.connect()
Explanation: In Node.js, the MongoClient.connect() function is used to connect to a MongoDB database.
15. What type of database is MongoDB?
- A) Relational
- B) Object-Oriented
- C) Document-Oriented
- D) Key-Value Store
Answer: C) Document-Oriented
Explanation: MongoDB is a document-oriented NoSQL database, which stores data in JSON-like documents.
16. What is the syntax for selecting a specific field in a MongoDB query?
- A) { field: 1 }
- B) { field: true }
- C) { field: "true" }
- D) { "field": true }
Answer: A) { field: 1 }
Explanation: To select a specific field in a MongoDB query, use { field: 1 } in the projection.
17. Which method is used to find all documents in a collection that match a specific query in MongoDB?
- A) findOne()
- B) findAll()
- C) find()
- D) query()
Answer: C) find()
Explanation: The find() method is used to retrieve documents that match a given query.
18. Which MongoDB method is used to update multiple documents?
- A) update()
- B) updateMany()
- C) modifyMany()
- D) setMany()
Answer: B) updateMany()
Explanation: updateMany() is used to update multiple documents in a collection.
19. What is the method to create an index in MongoDB?
- A) createIndex()
- B) create()
- C) addIndex()
- D) createNewIndex()
Answer: A) createIndex()
Explanation: The createIndex() method is used to create an index in a MongoDB collection.
20. In MongoDB, what data type is used to represent an object?
- A) String
- B) ObjectID
- C) Array
- D) Document
Answer: D) Document
Explanation: In MongoDB, data is represented as documents, which are stored as BSON (Binary JSON) format.
21. Which MongoDB method is used to count the number of documents in a collection?
- A) countDocuments()
- B) count()
- C) totalCount()
- D) documentCount()
Answer: A) countDocuments()
Explanation: countDocuments() is used to count the number of documents in a MongoDB collection.
22. Which operator is used for performing a logical AND in MongoDB?
- A) $and
- B) $or
- C) $andOperator
- D) $combine
Answer: A) $and
Explanation: The $and operator is used in MongoDB to perform logical AND operations in a query.
23. Which of the following is used to retrieve specific fields from documents in MongoDB?
- A) Projection
- B) Query Selector
- C) Aggregation
- D) Filter
Answer: A) Projection
Explanation: Projection is used to select specific fields in MongoDB queries.
24. What will the find() method return?
- A) A single document
- B) An array of documents
- C) A string of results
- D) A cursor to the documents
Answer: D) A cursor to the documents
Explanation: The find() method returns a cursor to the documents, which can be iterated over to access the results.
25. How do you insert multiple documents in MongoDB?
- A) insertBatch()
- B) insertMany()
- C) batchInsert()
- D) addDocuments()
Answer: B) insertMany()
Explanation: insertMany() is used to insert multiple documents at once in MongoDB.
26. What is the method used to remove a document in MongoDB?
- A) delete()
- B) remove()
- C) deleteOne()
- D) erase()
Answer: C) deleteOne()
Explanation: The deleteOne() method is used to remove a single document from MongoDB.
27. How do you specify a condition for a MongoDB query?
- A) condition()
- B) filter()
- C) where()
- D) query()
Answer: D) query()
Explanation: The query() method is used to specify a condition in MongoDB to filter documents.
28. Which of the following operators is used to check for values greater than a specified value in MongoDB?
- A) $gt
- B) $lt
- C) $eq
- D) $gte
Answer: A) $gt
Explanation: The $gt operator is used to find documents with values greater than a specified value.
29. Which method in MongoDB is used to modify a document?
- A) modify()
- B) updateOne()
- C) modifyOne()
- D) changeDocument()
Answer: B) updateOne()
Explanation: updateOne() is used to update a single document in MongoDB.
30. Which command is used to view the collections in a MongoDB database?
- A) show collections
- B) list collections
- C) display collections
- D) listTables
Answer: A) show collections
Explanation: In MongoDB, the show collections command is used to list all the collections in the current database.
31. What is a MongoDB Document?
- A) A single data item in a collection
- B) A group of collections
- C) A record in a table
- D) A row in a table
Answer: A) A single data item in a collection
Explanation: A MongoDB document is a single data item (record) stored in a collection, typically in BSON format.
32. What is MongoDB’s query language based on?
- A) SQL
- B) XQuery
- C) JavaScript
- D) Python
Answer: C) JavaScript
Explanation: MongoDB uses a query language based on JavaScript.
33. Which of the following commands is used to run an aggregation pipeline in MongoDB?
- A) aggregate()
- B) pipeline()
- C) group()
- D) collect()
Answer: A) aggregate()
Explanation: The aggregate() method is used to run aggregation pipelines in MongoDB.
34. What is MongoDB’s ObjectId?
- A) A unique identifier for each collection
- B) A data type used for storing dates
- C) A unique identifier for each document
- D) A method used to query documents
Answer: C) A unique identifier for each document
Explanation: The ObjectId is a unique identifier assigned to each document in MongoDB.
35. What is MongoDB Atlas?
- A) A cloud-based MongoDB service
- B) A MongoDB driver
- C) A database engine
- D) A query language
Answer: A) A cloud-based MongoDB service
Explanation: MongoDB Atlas is a fully-managed cloud service that hosts MongoDB databases.
36. How do you retrieve all documents where the age field is greater than 21 in MongoDB?
- A) find({ age: { $gte: 21 } })
- B) find({ age: 21 })
- C) find({ age: { $gt: 21 } })
- D) find({ age: { $lte: 21 } })
Answer: C) find({ age: { $gt: 21 } })
Explanation: The $gt operator is used to find documents where the age field is greater than 21.
37. Which of the following methods is used to count the number of documents returned by a query in MongoDB?
- A) countDocuments()
- B) count()
- C) totalDocuments()
- D) length()
Answer: A) countDocuments()
Explanation: The countDocuments() method returns the number of documents matching a query.
38. What is the default behavior of the findOne() method in MongoDB?
- A) It returns all documents
- B) It returns only the first matching document
- C) It returns a cursor
- D) It updates the document
Answer: B) It returns only the first matching document
Explanation: The findOne() method in MongoDB returns only the first document that matches the query.
39. Which of the following is used to compare values less than or equal to a specified value in MongoDB?
- A) $lte
- B) $gt
- C) $lt
- D) $eq
Answer: A) $lte
Explanation: The $lte operator is used to find values that are less than or equal to the specified value.
40. How do you add an index on a field in MongoDB?
- A) createIndex({ field: 1 })
- B) createIndex({ field: "index" })
- C) addIndex({ field: 1 })
- D) indexAdd({ field: 1 })
Answer: A) createIndex({ field: 1 })
Explanation: The createIndex() method is used to add an index on a field in MongoDB.
41. What is the result of the following query: db.collection.find({ age: { $gt: 21 } })?
- A) All documents with age greater than 21
- B) All documents with age less than 21
- C) The first document with age greater than 21
- D) The first document with age less than 21
Answer: A) All documents with age greater than 21
Explanation: The query retrieves all documents where the age field is greater than 21.
42. Which MongoDB method is used to retrieve a specific document by its unique identifier?
- A) find()
- B) findOne()
- C) query()
- D) get()
Answer: B) findOne()
Explanation: The findOne() method is used to retrieve a single document based on the query, such as by its unique identifier.
43. What does the following MongoDB query do: db.collection.find({ status: "active" })?
- A) Retrieves all documents with an "active" status
- B) Retrieves the first document with an "active" status
- C) Deletes documents with an "active" status
- D) Updates documents with an "active" status
Answer: A) Retrieves all documents with an "active" status
Explanation: The query retrieves all documents where the status field is "active".
44. Which of the following is used to aggregate documents in MongoDB?
- A) group()
- B) aggregate()
- C) mapReduce()
- D) sum()
Answer: B) aggregate()
Explanation: The aggregate() method is used to perform aggregation operations in MongoDB.
45. What is the default value of the writeConcern option in MongoDB?
- A) 0
- B) 1
- C) "majority"
- D) 2
Answer: B) 1
Explanation: The default writeConcern value in MongoDB is 1, which means that the write operation is acknowledged by the primary server.
46. Which operator is used for performing a logical OR operation in MongoDB?
- A) $or
- B) $and
- C) $not
- D) $combine
Answer: A) $or
Explanation: The $or operator is used to perform logical OR operations in MongoDB queries.
47. Which MongoDB method is used to find and replace a document?
- A) replaceOne()
- B) updateOne()
- C) findAndReplace()
- D) modify()
Answer: A) replaceOne()
Explanation: The replaceOne() method is used to find a document and replace it with another document.
48. What is the purpose of the MongoClient.connect() method in Node.js?
- A) To create a new MongoDB collection
- B) To connect to a MongoDB database
- C) To disconnect from the database
- D) To insert a document into the collection
Answer: B) To connect to a MongoDB database
Explanation: MongoClient.connect() is used to establish a connection to a MongoDB database in Node.js.
49. How do you limit the number of documents returned by a MongoDB query?
- A) limit()
- B) max()
- C) top()
- D) fetch()
Answer: A) limit()
Explanation: The limit() method is used to restrict the number of documents returned by a query.
50. Which method is used to create a new MongoDB collection?
- A) create()
- B) createCollection()
- C) newCollection()
- D) addCollection()
Answer: B) createCollection()
Explanation: The createCollection() method is used to create a new collection in MongoDB.
