Learn MongoDB MCQs

Prepare MongoDB MCQs (Multiple choice Questions) for exam and job interviews.

What is the command to create an aggregation pipeline in MongoDB?

1) db.collection_name.aggregate([])

2) db.collection_name.pipeline([])

3) db.collection_name.group([])

4) db.collection_name.createPipeline([])

Answer : Option 1

Which method is used to find documents with a specific value in a field?

1) db.collection_name.find({ field: value })

2) db.collection_name.query({ field: value })

3) db.collection_name.select({ field: value })

4) db.collection_name.get({ field: value })

Answer : Option 1

What is the purpose of the `$sample` stage in MongoDB aggregation?

1) Returns a random sample of documents

2) Sorts documents

3) Filters documents

4) Groups documents

Answer : Option 1

Which operator is used to match documents with array elements?

1) $elemMatch

2) $arrayMatch

3) $contains

4) $arrayIncludes

Answer : Option 1

Which command is used to set up sharding in MongoDB?

1) sh.enableSharding()

2) db.shardDatabase()

3) db.setupSharding()

4) shard.enable()

Answer : Option 1

What is the default write concern for MongoDB?

1) 1

2) 0

3) majority

4) all

Answer : Option 1

Which of the following methods is used to create a new collection in MongoDB?

1) db.createCollection()

2) db.collection_name.create()

3) db.create()

4) db.collection_name.new()

Answer : Option 1

What is the purpose of the `$out` stage in MongoDB aggregation?

1) Writes the output of the aggregation to a specified collection

2) Filters documents

3) Sorts documents

4) Groups documents

Answer : Option 1

Which command is used to find the distinct values for a field in a collection?

1) db.collection_name.distinct()

2) db.collection_name.unique()

3) db.collection_name.findUnique()

4) db.collection_name.getDistinct()

Answer : Option 1

What command is used to create an index in MongoDB?

1) db.collection_name.create()

2) db.collection_name.createIndex()

3) db.collection_name.index()

4) db.collection_name.addIndex()

Answer : Option 2