MongoDB - Create - Interview Questions

What is the significance of _id field in a MongoDB document? What happens if you insert a document without the _id field in a MongoDb collection?

 

_id is the field that uniquely identifies a document in the MongoDB collection. If you insert a document that does not contain the _id field, then MongoDB automatically generates the unique id.

In below example a MongoDB document without the _id field is inserted into a MongoDB collection. MongoDB generates a unique _id field for the document, inserts the document into the collection, and returns the result document containing the generated _id value.

> db.employees.insertOne({fname:'John', lname:'Doe', age:'25', 
title:'Manager', dept:'IT'})

{'acknowledged' :true,
'insertedId' : ObjectId('58479913fa42b4972b1efe40')}

What is the difference between the operations db.collection.insertOne() and db.collection.insertMany()?

 

db.collection.insertOne() – Inserts a single document into a MongoDB collection. It returns a document containing the inserted document’s _id field.

db.collection.insertMany() – Inserts a single document or multiple documents into a MongoDB collection. It returns a document containing each inserted document’s _id.

> db.employees.insertOne({fname:"John", lname:"Doe", age:"25", 
title:"Manager",dept:"IT"})

{
"acknowledged" : true,
"insertedId" : ObjectId("58479913fa42b4972b1efe40")
}
> db.employees.insertMany([{fname:"John", lname:"Doe", age:"25", 
title:"Manager", dept:"IT"},{fname:"Mike", lname:"Adams", age:"32",
title:"Director", dept:"IT"}])

{
"acknowledged" : true,
"insertedIds" : [
ObjectId("58479c2dfa42b4972b1efe46"),
ObjectId("58479c2dfa42b4972b1efe47")
]
}

What is the difference between the operations db.collection.insertMany() and db.collection.insert()?

 

db.collection.insertMany() – Inserts a single document or multiple documents into a MongoDB collection. It returns a document containing each inserted document’s _id.

db.collection.insert() – Inserts one or multiple documents into a MongoDB collection. It returns a BulkWriteResult object with status of the operation including details such as error, number of documents inserted, number of documents upserted etc.

> db.employees.insertOne({fname:"John", lname:"Doe", age:"25", 
title:"Manager",dept:"IT"})

{
"acknowledged" : true,
"insertedId" :
ObjectId("58479913fa42b4972b1efe40")
}
> db.employees.insert([{fname:"John", lname:"Doe", age:"25", 
title:"Manager", dept:"IT"},{fname:"Mike", lname:"Adams", age:"32",
title:"Director", dept:"IT"}])

BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

What is WriteResult object?

 

WriteResult object is an object returned by the db.collection.insertOne() and db.collection.insertMany() operations, which contains the object ids of the documents inserted by the operation.

What is BulkWriteResult object?

 

BulkWriteResult object is an object returned by the db.collection.insert() operation in which multiple documents are inserted. BulkWriteResult object contains status of the operation including details such as error, number of documents inserted, number of documents upserted etc.

Can a MongoDB document contain fields with array as value?

 

Yes, MongoDB document can have fields with values of type array. The array elements can be single values or can be documents.

//field "skills" has array of values
> db.employees.insertOne({fname:"Dave", lname:"Smith", age:"30",
title:"Manager", dept:"IT",
skills:[“Java”,”Oracle”,”People Management”,”Project Management”])
//field "skills" has array of documents 
> db.employees.insertOne({fname:"John", lname:"Smith", age:"32",
title:"Manager", dept:"IT",
skills: [{skill: “Java”, exp: “10”}, {skill: “Oracle”, exp: “10”},
{skill: “MongoDb”, exp: “10”}, {skill: “BigData”, exp: “10”}])

Can a MongoDB document contain embedded documents?

 

Yes, MongoDB document can have fields that hold embedded documents. For example - in below document the field ‘address’ contains an embedded document. The field 'skills' is of type array and contains elements which are documents.

> db.employees.insertOne({fname:"John", lname:"Smith", age:"32", 
title:"Manager",dept:"IT",
address: {line1:”1111 broadway”, line2:”Flat# 203”,
city:”New York”, state:”NY”, country:”USA”},
skills: [{skill: “Java”, exp: “10”}, {skill: “Oracle”, exp: “10”},
{skill: “MongoDb”, exp: “10”}, {skill: “BigData”, exp: “10”}])
 
Important Keywords to Remember

Significance of _id field in MongoDB documents
db.collection.insertOne()
db.collection.insertMany()
db.collection.insert()
WriteResult Object
BulkWriteResult Object
MongoDB documents containing arrays
MongoDB documents containing documents
Subscribe to our Questions