i have document 1 below. periods field defines periods user had access data.
{ "_id":objectid("51878ecc0e9528429ab6e7cf"), "name" : "peter flack", "periods":[ { "from":1, "to":2 }, { "from":4, "to":6 }, { "from":10, "to":12 } ] } now want determine if arbitrary period within of periods in period field. have tried following:
db.subs.find({"periods.from" : {$gte : 1}, "periods.to" : {$lte : 12}, "periods.from" : {$lte : 12}, "periods.to" : {$gte : 1}}) this query returns document, since there 1 element in array from >= 1 , antoher element to <= 12.
but, want document if both from , to in same element match criteria.
how write query achieve this?
you have use $elemmatch : elemmatch match params in each element of array.
mongo > db.subs.find({periods : {$elemmatch : { : {$gte : 1, $lte:12}, : {$gte : 1, $lte : 12} }}}).pretty() { "_id" : objectid("51878ecc0e9528429ab6e7cf"), "name" : "peter flack", "periods" : [ { "from" : 1, "to" : 2 }, { "from" : 4, "to" : 6 }, { "from" : 10, "to" : 12 } ] } mongo > db.subs.find({periods : {$elemmatch : { : {$gte : 2, $lte : 4}, : {$gte : 10, $lte : 12} }}}).pretty() no result
Comments
Post a Comment