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.


Tuesday, 1 December 2015

MongoDB Interview Questions and Answer on Database

MongoDB Interview Questions and Answer on Database

Question: What is Relationships in MongoDb?
Relationships in MongoDb represent how various documents are related to each other.
For Example:
There are various documents like user, address, payment details etc.
Connection between these documents know as Relationships.


Question: What is Two type Relationships?
  1. Modeling Embedded Relationships
  2. Modeling Referenced Relationships



Question: What is Modeling Embedded Relationships?
In this Approach, we embed the one document into other.
For Example:
{
   "_id":ObjectId("521234cd85242f436000007"),
   "contact": "987685555555",
   "dob": "01-01-1992",
   "name": "User1",
   "address": [
      {         
         "pincode": 160011,
         "city": "Mohali",
       
      },
      {
         "pincode": 140301,
         "city": "Mohali",
      }
   ]
}

We have added the both address in in user document.


Question: Modeling Referenced Relationships?
In this approach, We add the referenceId instead of whole document, thats why know as Referenced Relationships.
For Eample:
{
   "_id":ObjectId("521234cd85242f436000007"),
   "contact": "987685555555",
   "dob": "01-01-1992",
   "name": "User1",
   "address": [
      ObjectId("521234ce85242f436000001"),
      ObjectId("521234ce85242f436000002")
   ]
}


Above is also know as Manual References because we put the RefereceId static.


Question: What is different between "Manual References" and "DBRefs References"?
Manual References
where you save the _id field of one document in another document as a reference know as Manual References.
Just see the above question for Example.

DBRefs References
References the one document with another using the value of first document's _id field, collection name, and database name.



Question: Explain the DBRefs References in detail?
There are three fields in DBRefs:
$ref: This field specifies the collection of the referenced document.
$id: This field specifies the _id field of the referenced document.
$db: Database name where referenced document lies. This is Optional.
{
   "_id":ObjectId("521234cd85242f436000007"),
   "address": {
   "$ref": "address_home",
   "$id": ObjectId("521234cd85242f436000007"),
   "$db": "webtechnology"},
   "contact": "987685555555",
   "dob": "01-01-1991",
   "name": "web-tech"
} 



Question: What is a Covered Query?
Covered query is a query in which all the "fields of Query" and "fields returned in the query" are same index.
For Example:
{
   "_id":ObjectId("521234cd85242f436000007"),
   "address": {
   "$ref": "address_home",
   "$id": ObjectId("521234cd85242f436000007"),
   "$db": "webtechnology"},
   "contact": "987685555555",
   "dob": "01-01-1991",
   "name": "web-tech"
} 

Add "Index"
db.users.ensureIndex({name:1,usercontact:1})


If we are using below:
db.users.find({name:"web-tech"},{usercontact:"987685555555",_id:0})

This is best example for Covered Query.




Question: How to Analyzing the queries in MongoDB?
$explain: The $explain operator provides information on the query, indexes and other statistics. For analyse the use just add .explain() at the end of query.
db.users.find({name:"web-tech"}).explain()


Question: What is use of $hint operator?
$hint: operator is used to forces the query optimizer to use the specified index to run a query.
It is useful when you want to test performance of a query with different indexes with applying the index in real.
db.users.find({name:"web-tech"}).hint({dob:"01-01-2015"}).explain()



Question: Does MongoDb provides atomic transactions?
Yes, but provides only for single document.
In this way, Either it update all fields or none.
db.myproducts.findAndModify({ 
   query:{_id:2,product_available:{$gt:0}}, 
   update:{ 
      $inc:{product_available:-1}, 
      $push:{product_bought_by:{customer:"webtech",date:"19-Jan-2014"}} 
   }    
})



Question: How to add index on document?
db.users.ensureIndex({"dob":1})




Question: How to add index on sub document?
db.users.ensureIndex({"address.city":1,"address.state":1})