Query for documents with given set of attributes in MongoDB -
i'm writing query in mongo.exe follows:
db.sheep.find( { "timestamp" : { "$gt":isodate("2015-05-15t10:00:00.000z"), "$lt":isodate("2015-05-15t10:05:10.000z") } }, {"x":1,"y":1,"z":1,"timestamp":1})
however, of returned documents have timestamp
no x
, y
, z
. how return documents have both timestamp
, x y z
?
i presume there pretty straightforward logic add query can't seem find when searching around.
the first parameter, filter object, determines criteria match must fulfill. in case, timestamp must in interval.
the second parameter projection, used remove fields unnecessary, if data large (like embedded binary document, file or something).
if want make sure documents have x
, y
, z
set, can check using $exists
:
db.sheep.find( { "timestamp" : { "$gt" : isodate("2015-05-15t10:00:00.000z"), "$lt" : isodate("2015-05-15t10:05:10.000z") }, "x": { "$exists": true }, "y": { "$exists": true }, "z": { "$exists": true } });
however, warned sign of bad schema design. $exists
, definition, has worst-case selectivity, don't expect these queries fast.
Comments
Post a Comment