Question: What are the Comparison Operators in MongoDB? Compare them with RDBMS?
Operation | Syntax | Example | RDBMS 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
Expression | Description | MongoDB Example |
---|---|---|
$addToSet | Inserts the value to an array in the resulting document but does not create duplicates. | db.mycollection.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}]) |
$first | Gets 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"}}}]) |
$last | Gets 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"}}}]) |
$sum | Sums up the defined value from all documents. | db.mycollection.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}]) |
$avg | Calculates the average of all given values from all documents. | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}]) |
$min | Gets the minimum of the corresponding values from all documents. | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}]) |
$max | Gets the maximum of the corresponding values from all documents. | db.mycollection.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}]) |
$push | Inserts 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?
- To keep your data safe
- High availability of data in 24*7*365
- No downtime for maintenance
- Read scaling (extra copies to read from)
- Disaster Recovery
- 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.