More Related Content Similar to Mongo db query docuement (20) Mongo db query docuement2. Query Document Conditional
db.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. 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. 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. 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. Using $SLICE
Get the last three comments
> db.user.findOne({}, , {“remarks" :
{"$slice" : -3}}
Get the first three comments
> db.user.findOne({}, , {“remarks" :
{"$slice" : 3}}
7. CURSOR
SIMILAR to implicit cursor in Oracle, give a
refrence pointer to the results untill fetch
the result will not be stored in the memory
For example : --
> var cursor = db.user.find()
> cursor.forEach(function(x) { print(x._id) } )
ObjectId("513c6ef1cfce9090d3fd8b1d")
ObjectId("513e1339cb4ee0901c14db28")
ObjectId("513e1340cb4ee0901c14db29")