Which of the following commands is used to insert a single document into a collection?
A
db.collection.insertMany() B
db.collection.addOne() C
db.collection.insertOne() D
db.collection.save()
Analysis & Theory
`insertOne()` is used to insert a single document into a collection.
Which method is used to find documents in MongoDB?
A
db.collection.find() B
db.collection.search() C
db.collection.select() D
db.collection.query()
Analysis & Theory
`find()` is used to retrieve documents from a MongoDB collection.
Which command is used to delete a single document from a collection?
A
db.collection.removeOne() B
db.collection.deleteOne() C
db.collection.destroyOne() D
db.collection.eraseOne()
Analysis & Theory
`deleteOne()` deletes the first matching document from the collection.
What will `db.collection.updateOne({name: 'John'}, {$set: {age: 30}})` do?
A
Update all documents with name 'John' B
Delete the document with name 'John' C
Update the first document with name 'John', setting age to 30 D
Insert a new document if 'John' is not found
Analysis & Theory
`updateOne()` updates the first matching document, setting the specified field(s).
Which of the following is used to create an index in MongoDB?
A
db.collection.index() B
db.collection.ensureIndex() C
db.collection.createIndex() D
db.collection.addIndex()
Analysis & Theory
`createIndex()` is used to create indexes in a MongoDB collection.
How do you count the number of documents in a collection?
A
db.collection.count() B
db.collection.countDocuments() C
db.collection.size() D
db.collection.length()
Analysis & Theory
`countDocuments()` gives the count of documents matching the query or all if no filter is passed.
What is the purpose of the `$set` operator in MongoDB?
A
To delete fields B
To rename a collection C
To replace the entire document D
To update specific fields
Analysis & Theory
`$set` is used to modify values of existing fields or add new fields if they don't exist.
What does `db.collection.drop()` do?
A
Deletes a document B
Deletes all documents in the collection C
Deletes the entire collection D
Deletes the database
Analysis & Theory
`drop()` deletes the entire collection and all its data.
Which of the following is a valid MongoDB comparison operator?
A
$greater B
$eq C
$equals D
$compare
Analysis & Theory
`$eq` is the correct comparison operator for 'equals'.
How do you sort documents in descending order by the 'age' field?
A
db.collection.sort({'age': -1}) B
db.collection.find().sort({'age': -1}) C
db.collection.find({'age': -1}) D
db.collection.find().order({'age': -1})
Analysis & Theory
Sorting is done by appending `.sort({'age': -1})` to the `find()` query.