MCQs Of Day 30 Project Development (MongoDB Final Project)
1. Which of the following is a NoSQL database?
A) MySQL
B) MongoDB
C) Oracle
D) PostgreSQL
Answer: B) MongoDB
Explanation: MongoDB is a NoSQL database, while MySQL, Oracle, and PostgreSQL are relational databases.
2. What is the default port number for MongoDB?
A) 2700
B) 2701
C) 27017
D) 8080
Answer: C) 27017
Explanation: The default port for MongoDB is 27017.
3. Which command is used to connect to a MongoDB database from the MongoDB shell?
A) connect
B) mongo
C) db.connect()
D) use
Answer: B) mongo
Explanation: The mongo command is used to connect to MongoDB from the shell.
4. Which MongoDB operation is used to insert a document into a collection?
A) db.collection.insert()
B) db.collection.insertOne()
C) db.collection.add()
D) db.collection.push()
Answer: B) db.collection.insertOne()
Explanation: insertOne() is used to insert a single document into a MongoDB collection.
5. Which of the following is NOT a valid MongoDB data type?
A) String
B) Boolean
C) Function
D) Date
Answer: C) Function
Explanation: MongoDB supports basic data types like String, Boolean, and Date, but does not have a native data type for Function.
6. In MongoDB, which of the following is used to define the schema of a collection?
A) Model
B) Schema
C) Table
D) Document
Answer: B) Schema
Explanation: In MongoDB, a schema is defined using the Schema class (e.g., Mongoose provides this feature in Node.js).
7. Which command is used to delete a document in MongoDB?
A) delete()
B) remove()
C) deleteOne()
D) drop()
Answer: C) deleteOne()
Explanation: deleteOne() is used to delete a single document that matches the specified filter.
8. Which of the following methods in MongoDB is used to fetch all documents from a collection?
A) findAll()
B) fetch()
C) find()
D) get()
Answer: C) find()
Explanation: The find() method is used to retrieve all documents from a MongoDB collection.
9. What does the mongoose.connect() function do in Node.js?
A) Establishes a connection to the MongoDB server
B) Creates a new database
C) Creates a new collection
D) Starts the MongoDB server
Answer: A) Establishes a connection to the MongoDB server
Explanation: mongoose.connect() is used to establish a connection to the MongoDB server.
10. Which method is used to update a document in MongoDB?
A) updateOne()
B) save()
C) modify()
D) put()
Answer: A) updateOne()
Explanation: updateOne() is used to update a single document in MongoDB.
11. What is the primary advantage of MongoDB over relational databases?
A) Stronger data consistency
B) Ability to handle large amounts of data in a flexible, schema-less format
C) Support for complex joins
D) Faster transactions
Answer: B) Ability to handle large amounts of data in a flexible, schema-less format
Explanation: MongoDB allows flexible schema and handles large datasets effectively, which is a primary advantage over relational databases.
12. Which of the following is used to handle relationships between documents in MongoDB?
A) Foreign keys
B) References
C) Joins
D) Primary keys
Answer: B) References
Explanation: MongoDB handles relationships using references between documents.
13. Which of the following is the proper syntax to create a new instance of a model in Mongoose?
A) const task = new Task()
B) Task.create()
C) Task.new()
D) task.createNew()
**Answer: A) const task = new Task()** **Explanation:** To create a new instance of a model in Mongoose, you use new ModelName()`.
14. Which Mongoose method is used to save a document to the database?
A) save()
B) insert()
C) store()
D) add()
Answer: A) save()
Explanation: save() is used to save a document to the MongoDB database.
15. What is the purpose of MongoDB Atlas?
A) Local server hosting
B) Cloud-based MongoDB database management
C) SQL database management
D) Code versioning
Answer: B) Cloud-based MongoDB database management
Explanation: MongoDB Atlas is a cloud-based service for managing MongoDB databases.
16. Which command is used to update multiple documents in a MongoDB collection?
A) updateMany()
B) modifyMany()
C) updateAll()
D) editMany()
Answer: A) updateMany()
Explanation: updateMany() is used to update multiple documents in a MongoDB collection.
17. Which of the following is true about MongoDB’s data model?
A) MongoDB uses tables and rows.
B) MongoDB stores data in collections and documents.
C) MongoDB uses SQL for querying.
D) MongoDB uses only key-value pairs.
Answer: B) MongoDB stores data in collections and documents.
Explanation: MongoDB uses collections to store documents, which is different from relational databases that use tables and rows.
18. Which function is used to retrieve a single document based on a specific condition in MongoDB?
A) findOne()
B) get()
C) fetch()
D) retrieve()
**Answer: A) findOne()** **Explanation:** findOne()` is used to retrieve a single document based on a specified condition.
19. Which method is used in MongoDB to remove all documents from a collection?
A) drop()
B) clear()
C) removeAll()
D) truncate()
**Answer: A) drop()** **Explanation:** drop()` is used to remove all documents and delete the entire collection in MongoDB.
20. Which of the following methods is used to create a new collection in MongoDB?
A) create()
B) insert()
C) add()
D) use
**Answer: A) create()** **Explanation:** In MongoDB, you can create a collection using the create()` method.
21. Which of the following is a correct syntax for creating an Express server in Node.js?
A) const app = express.create();
B) const app = new express();
C) const app = express();
D) const app = new express.create();
Answer: C) const app = express();
Explanation: The correct syntax to create an Express server is const app = express();.
22. In a Node.js application, how do you send a JSON response from an Express route?
A) res.sendJSON()
B) res.json()
C) res.send()
D) res.response()
**Answer: B) res.json()** **Explanation:** res.json()` is used to send a JSON response from an Express route.
23. Which method in MongoDB is used to sort documents based on a field?
A) sortBy()
B) orderBy()
C) sort()
D) arrange()
**Answer: C) sort()** **Explanation:** sort()` is used to order documents in MongoDB based on a specified field.
24. What is the role of ObjectId in MongoDB?
A) It is used to uniquely identify documents in a collection.
B) It is used to store dates.
C) It is used to store strings.
D) It is used to reference external collections.
Answer: A) It is used to uniquely identify documents in a collection.
Explanation: ObjectId is a unique identifier automatically assigned to each document in MongoDB.
25. In MongoDB, which of the following is used to model one-to-many relationships between collections?
A) Foreign key
B) Subdocuments
C) Primary key
D) Tables
Answer: B) Subdocuments
Explanation: Subdocuments are used to model one-to-many relationships in MongoDB.
26. Which HTTP method is commonly used to retrieve data from a backend API?
A) POST
B) PUT
C) GET
D) DELETE
Answer: C) GET
Explanation: The GET method is used to retrieve data from an API.
27. Which of the following MongoDB operations is used to find documents matching specific criteria?
A) find()
B) search()
C) lookup()
D) retrieve()
**Answer: A) find()** **Explanation:** find()` is used to search for documents in a collection that match specified criteria.
28. What is the main purpose of using Mongoose in a Node.js application?
A) To handle HTTP requests
B) To manage MongoDB connections and queries
C) To serve static files
D) To handle user authentication
Answer: B) To manage MongoDB connections and queries
Explanation: Mongoose is an ODM (Object Data Modeling) library for MongoDB, used to manage database connections and queries in Node.js.
29. Which MongoDB method is used to add multiple documents to a collection at once?
A) insertMany()
B) addAll()
C) insertBatch()
D) createAll()
**Answer: A) insertMany()** **Explanation:** insertMany()` is used to insert multiple documents at once into a MongoDB collection.
30. In MongoDB, a document is stored in a collection that is similar to a:
A) Table in a relational database
B) Row in a relational database
C) Column in a relational database
D) Schema in a relational database
Answer: A) Table in a relational database
Explanation: A MongoDB collection is similar to a table in a relational database, while documents are similar to rows.
(Continued in the next message)
4o mini
You said:
yes
ChatGPT said:
ChatGPT
31. Which of the following is a valid MongoDB query operator?
A) LIKE
B) EQUALS
C) $eq
D) IS
Answer: C) $eq
Explanation: The $eq operator is used in MongoDB queries to check for equality (similar to the = operator in SQL).
32. What is the purpose of the find() method in MongoDB?
A) To find all the documents in a collection
B) To find a specific document by its ID
C) To find documents based on a query
D) All of the above
Answer: D) All of the above
Explanation: The find() method can be used to find all documents, a specific document by ID, or documents that match a query.
33. What is a common advantage of using MongoDB for project development?
A) It supports only structured data
B) It requires fewer resources compared to SQL databases
C) It supports horizontal scaling
D) It requires complex table joins
Answer: C) It supports horizontal scaling
Explanation: MongoDB is known for its ability to scale horizontally, making it efficient for handling large datasets.
34. Which of the following is true about MongoDB’s schema-less nature?
A) Documents in a collection must follow a predefined schema
B) All documents in a collection can have different structures
C) Schema enforcement is required in MongoDB
D) Data types must be consistent across all documents
Answer: B) All documents in a collection can have different structures
Explanation: MongoDB is schema-less, meaning that documents in the same collection can have different fields and structures.
35. Which of the following methods is used to add a new field to an existing document in MongoDB?
A) updateOne() with $set
B) insertOne()
C) addField()
D) push()
Answer: A) updateOne() with $set
Explanation: The updateOne() method with the $set operator is used to add or modify fields in an existing document.
36. Which of the following is used to perform aggregation operations in MongoDB?
A) find()
B) aggregate()
C) lookup()
D) map()
**Answer: B) aggregate()** **Explanation:** The aggregate()` method is used to perform aggregation operations such as summing, counting, grouping, etc.
37. Which of the following is true about MongoDB's data model?
A) Data is stored in tables and rows
B) Each record is represented by a document, and documents are stored in collections
C) MongoDB supports only key-value pairs
D) MongoDB uses SQL for queries
Answer: B) Each record is represented by a document, and documents are stored in collections
Explanation: MongoDB stores data in documents, which are grouped into collections, unlike SQL databases that use tables and rows.
38. Which MongoDB method is used to group documents together based on a specific condition?
A) group()
B) aggregate()
C) lookup()
D) sort()
**Answer: B) aggregate()** **Explanation:** The aggregate()` method allows you to group documents together based on certain conditions.
39. What does the ObjectId field in MongoDB represent?
A) A reference to another document
B) A unique identifier for a document
C) A collection's name
D) A timestamp of the document's creation
Answer: B) A unique identifier for a document
Explanation: The ObjectId field is automatically generated by MongoDB and uniquely identifies a document in a collection.
40. Which of the following best describes a "document" in MongoDB?
A) A row in a table
B) A set of nested collections
C) A JSON-like structure with field and value pairs
D) A database table
Answer: C) A JSON-like structure with field and value pairs
Explanation: A MongoDB document is a JSON-like structure consisting of key-value pairs, making it flexible and schema-less.
41. Which of the following methods is used to remove all documents from a collection?
A) remove()
B) clear()
C) truncate()
D) deleteAll()
**Answer: A) remove()** **Explanation:** The remove()` method is used to delete documents from a collection. You can specify a filter to delete specific documents or remove all documents.
42. Which of the following is the default data type for storing dates in MongoDB?
A) String
B) Boolean
C) Date
D) Timestamp
Answer: C) Date
Explanation: MongoDB uses the Date data type to store date and time information.
43. Which of the following methods is used to connect a Node.js application to a MongoDB database using Mongoose?
A) mongoose.connect()
B) mongoose.link()
C) mongoose.connectToDatabase()
D) mongoose.db()
**Answer: A) mongoose.connect()** **Explanation:** The mongoose.connect()` method is used to establish a connection to the MongoDB database.
44. What is the purpose of a middleware in Express.js?
A) To manage the database connections
B) To handle HTTP request/response cycles
C) To serve static files
D) To perform data validation
Answer: B) To handle HTTP request/response cycles
Explanation: Middleware in Express.js is used to handle requests and responses, allowing for data validation, logging, authentication, and more.
45. What is the correct MongoDB operator to check for documents with a specific field value greater than a given value?
A) $gt
B) $lt
C) $gte
D) $eq
Answer: A) $gt
Explanation: The $gt (greater than) operator is used in MongoDB queries to find documents with field values greater than a specified value.
46. Which of the following is used to reference a foreign key from one document to another in MongoDB?
A) Subdocument
B) ObjectId
C) ForeignKey
D) DBReference
Answer: B) ObjectId
Explanation: In MongoDB, an ObjectId is used to reference other documents in the database.
47. Which of the following methods is used to find the number of documents in a collection in MongoDB?
A) countDocuments()
B) length()
C) size()
D) findAll()
**Answer: A) countDocuments()** **Explanation:** countDocuments()` is used to count the number of documents in a collection based on a filter.
48. Which of the following is NOT a valid data type in MongoDB?
A) String
B) Integer
C) Array
D) Blob
Answer: D) Blob
Explanation: MongoDB supports data types such as String, Integer, and Array, but Blob is not a valid type in MongoDB.
49. Which of the following is a valid MongoDB aggregation operator used to filter documents after grouping?
A) $filter
B) $match
C) $where
D) $group
Answer: B) $match
Explanation: $match is used in MongoDB aggregation to filter documents after they have been grouped.
50. What is the purpose of the use() method in Express.js?
A) To handle HTTP requests
B) To handle middleware functions
C) To start the server
D) To configure the database
Answer: B) To handle middleware functions
Explanation: use() is used in Express.js to define middleware functions for handling requests and responses in the application.
