Mongo db query docuement

110 views
98 views

Published on

Visit
http://www.zariga.com
http://www.youtube.com/user/zarigatongy

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
110
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mongo db query docuement

  1. 1. MongoDB QueryQuery Document Use Case detailed
  2. 2. Query Document Conditionaldb.user.find({"age" : {"$gte" : 8, "$lte" :23}})db.user.find({"age" : {"$gte" : 8}})db.user.find({"age" : {"$gt" : 7}})db.user.find({"age" : {"$lt" : 23}})Mongo Variable Operator equivalent"$lt" <"$lte" <="$gt” >"$gte" >=
  3. 3. Using null> db.user.find(){ "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "date" : ISODate("2013-03-10T11:30:50.555Z"),"remarks" : [ { "name" : "anish", "rate" : "good" }, { "name" : "nath", "rate" : "bad" } ], "uName" : "anish" }{ "_id" : ObjectId("513e1339cb4ee0901c14db28"), "uName" : “ashish", "mName" : "", "date" :ISODate("2013-03-11T17:23:59.294Z")}{ "_id" : ObjectId("513e1340cb4ee0901c14db29"), "uName" : “arjun", "mName" : null, "date" :ISODate("2013-03-11T17:24:14.971Z") }> db.user.find({"mName" : null}){ "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "date" : ISODate("2013-03-10T11:30:50.555Z"),"remarks" : [ { "name" : "anish", "rate" : "good" }, { "name" : "nath", "rate" : "bad" } ], "uName" : "anish" }{ "_id" : ObjectId("513e1340cb4ee0901c14db29"), "uName" : “arjun", "mName" : null, "date" :ISODate("2013-03-11T17:24:14.971Z") }* doesnot exist also return "$exists“ to rescue
  4. 4. Using null with $EXISTS> db.user.find({"mName" : {"$in" : [null], "$exists" : true}}){ "_id" :ObjectId("513e1340cb4ee0901c14db29"),"uName" : "anish", "mName" : null, "date" :ISODate("2013-03-11T17:24:14.971Z") }
  5. 5. Using $SLICE> db.user.findOne(){"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),"date" : ISODate("2013-03-10T11:30:50.555Z"),"remarks" : [{"name" : "anish","rate" : "good"},{"name" : "nath","rate" : "bad"}],"uName" : "anish"}>db.user.findOne({}, {"remarks" : {"$slice" : -1}})Will get the last comment{"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),"date" : ISODate("2013-03-10T11:30:50.555Z"),"remarks" : [{"name" : "nath","rate" : "bad"}],"uName" : "anish"}
  6. 6. Using $SLICEGet the last three comments> db.user.findOne({}, , {“remarks" :{"$slice" : -3}}Get the first three comments> db.user.findOne({}, , {“remarks" :{"$slice" : 3}}
  7. 7. CURSORSIMILAR to implicit cursor in Oracle, give arefrence pointer to the results untill fetchthe result will not be stored in the memoryFor example : --> var cursor = db.user.find()> cursor.forEach(function(x) { print(x._id) } )ObjectId("513c6ef1cfce9090d3fd8b1d")ObjectId("513e1339cb4ee0901c14db28")ObjectId("513e1340cb4ee0901c14db29")
  8. 8. Thanks for Watchingfeedbackappreciated…………………………

×