Wednesday, 2 December 2015

Advance MongoDB Interview Questions and Answer

Advance MongoDB Interview Questions and Answer



Question: What are the Comparison Operators in MongoDB? Compare them with RDBMS?
OperationSyntaxExampleRDBMS Equivalent
Greater Than{<key>:{$gt:<value>}}db.mycollection.find({"likes":{$gt:50}}).pretty()where likes > 50
Greater Than Equals{<key>:{$gte:<value>}}db.mycollection.find({"likes":{$gte:50}}).pretty()where likes >= 50
Equality{<key>:<value>}db.mycollection.find({"by":"web-tech"}).pretty()where by = 'web-tech'
Less Than{<key>:{$lt:<value>}}db.mycollection.find({"likes":{$lt:50}}).pretty()where likes < 50
Less Than Equals{<key>:{$lte:<value>}}db.mycollection.find({"likes":{$lte:50}}).pretty()where likes <= 50
Not Equals{<key>:{$ne:<value>}}db.mycollection.find({"likes":{$ne:50}}).pretty()where likes != 50



Question: What is Aggregation operations in MongoDB?
Aggregation operations are functions which are used to group the results. For Example count(*), sum(*) are aggregate functions in RDBMS.

Following are Aggregation operations in MongoDB
ExpressionDescriptionMongoDB Example
$addToSetInserts the value to an array in the resulting document but does not create duplicates.db.mycollection.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])
$firstGets the first document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage.db.mycollection.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}])
$lastGets the last document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage.db.mycollection.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])
$sumSums up the defined value from all documents.db.mycollection.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])
$avgCalculates the average of all given values from all documents.db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])
$minGets the minimum of the corresponding values from all documents.db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
$maxGets the maximum of the corresponding values from all documents.db.mycollection.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])
$pushInserts the value to an array in the resulting document.db.mycollection.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])

For Example:
db.mycollection.find({$or:[{"by":"webtechnology"},{"title": "MongoDB"}]}).pretty()



Question: What is Replication in MongoDB?
Replication is the process of synchronizing data across multiple servers. In this we update the data in all servers.
Replication increase the redundancy and but also increases data availability with multiple copies of data on different database servers.


Question: What is benefits Replication in MongoDB?
  1. To keep your data safe
  2. High availability of data in 24*7*365
  3. No downtime for maintenance
  4. Read scaling (extra copies to read from)
  5. Disaster Recovery
  6. Fast



Question: How to setup the Auto-increment fields (like 1,2,34, etc) for a document?
db.mycollection.insert({_id:"productid",sequence_value:0});

The field sequence_value keeps track of the last value of the sequence.


Question: How to Creating Javascript Function?
function getNextSequenceValue(sequenceName){
   var sequenceDocument = db.counters.findAndModify({
      query:{_id: sequenceName },
      update: {$inc:{sequence_value:1}},
      new:true
   }); 
   return sequenceDocument.sequence_value;
}



Question: What is syntax for update the document?
db.mycollection.update(SELECTIOIN_CRITERIA, UPDATED_DATA)



Question: What is syntax for delete the document?
db.mycollection.remove(DELLETION_CRITTERIA)



Question: What is syntax for delete all the document?
db.mycollection.remove()

In this we will not pass criteria for search.


Question: How to sort a document by title in ascending order?
db.mycollection.find().sort({"title":1})



Question: What is MongoDB Projection?
MongoDB projection meaning is selecting only necessary data rather than selecting whole of the data of a document.


Question: What is Capped collections?
Capped collections are fixed-size circular collections that follow the insertion order to high performance for every action.


Question: How to create Capped Collection?
db.createCollection("mycollection",{capped:true,size:100000})



Question: How to check if collection is Capped OR Not?

db.mycollection.isCapped()



Question: What is GridFS? How it is used?
GridFS is the just specification in MongoDB.
GridFS is used for storing and retrieving large files such as images, audio files, video files etc.


Question: What is Rockmongo?
Rockmongo is a MongoDB administration tool using which you can manage your server, databases, collections, documents, indexes etc.