21. Which command creates a new collection in MongoDB?
- a) db.createCollection()
- b) db.newCollection()
- c) db.collection.create()
- d) db.addCollection()
db.createCollection("name") explicitly creates a new collection.
Test your knowledge of Database - [MongoDB] section with these interactive multiple-choice questions.
21. Which command creates a new collection in MongoDB?
db.createCollection("name") explicitly creates a new collection.
22. What is the purpose of the $unset operator in MongoDB?
{ $unset: { field: "" } } removes the specified field.
23. Which method returns a single document in MongoDB?
findOne() returns the first document matching the query.
24. What does the $addToSet operator do?
$addToSet adds elements to an array only if they don't already exist.
25. Which command displays MongoDB version?
26. What is the purpose of the $project stage in aggregation?
$project reshapes documents by including/excluding fields.
27. Which operator performs a logical OR operation?
$or: [ {cond1}, {cond2} ] matches documents satisfying any condition.
28. What is the purpose of the $text operator?
$text performs text search on fields with a text index.
29. Which method counts documents in a collection?
countDocuments() returns the count matching the query.
30. What does the $slice operator do?
$slice limits array elements during projection.
31. Which command displays current database stats?
db.stats() shows storage size, collections count, etc.
32. What is the purpose of the $regex operator?
$regex provides regular expression capabilities for pattern matching.
33. Which method updates all matching documents?
updateMany() updates all documents matching the filter.
34. What does the $elemMatch operator do?
$elemMatch matches documents where at least one array element matches all criteria.
35. Which command drops a database in MongoDB?
db.dropDatabase() deletes the current database.
36. What is the purpose of the $sort stage in aggregation?
$sort arranges documents in ascending or descending order.
37. Which operator increments a field's value?
{ $inc: { field: 1 } } increments the field by the specified amount.
38. What does the $out stage do in aggregation?
$out writes aggregation results to a specified collection.
39. Which method replaces a document?
replaceOne() completely replaces the matched document.
40. What is the purpose of the $bucket stage in aggregation?
$bucket categorizes documents into groups based on specified boundaries.