I’m trying to get the values from an array of objects for the keys that match certain criteria. For the objects in the array the keys will be longs and the values strings. Here’s a sample MondgoDB document:
"_id" : ObjectId("509eba6d84f30613b4aee1ca"),
"timestamps" : [
{
"1234" : "ABC"
},
{
"2345" : "DEF"
},
{
"3456" : "GHI"
},
{
"4567" : [
"JKL",
"ABC"
]
},
{
"5678" : "GHI"
}
],
"word" : "foo"
For example I’d like to retrieve the values of all “timestamps” entries where the key is less than 3000 (i.e. “ABC” and “DEF” in the above). I’ve only had luck in finding which documents in the collection have specific keys by using coll.find({"timestamps.4567":{$exists:true}}) but I get no results when trying things like coll.find({"timestamps":{$lt:3000}}) – I’m obviously missing something there that would check if timestamps’ keys are less than 3000, not the value of timestamps itself.
Maybe I got it all wrong… looks like you need to alter a bit the structure of your documents:
and then you can query using
elemMatch:Make sure you have an index on
timestamps.keyHTH