ruby on rails - Find document in subarray in mongodb -
i have many collections such this
{ "_id" : 383566, "horses" : { "609075" : { "_id" : 609075, "name" : "aghawonan" }, "634478" : { "_id" : 634478, "name" : "french thyne" }, "595535" : { "_id" : 595535, "name" : "doctor hanley" }, "593718" : { "_id" : 593718, "name" : "ballyhill boy" }, "578089" : { "_id" : 578089, "name" : "oh alabama" }, "632597" : { "_id" : 632597, "name" : "bowling alone" }, "525424" : { "_id" : 525424, "name" : "tally-ho duke" } }
i need find collections in array horses _id have match
my version, not work
db.cards.find({'horses': {$elemmatch: {_id:525424}}})
in example, horses
not array, object. data structure bit odd , redundant because id both part of 'name' (or path if will) , actual document data. in data structure, refer particular horse horses.609075.name
, not helpful.
your query work if horses
array so:
{ "_id" : 383566, "horses" : [ { "_id" : 609075, "name" : "aghawonan" }, { "_id" : 634478, "name" : "french thyne" }, // ... ] }
since you're querying single property, $elemmatch
isn't required. query this:
db.cards.find({"horses._id" : 609075});
please note you'll receive entire card result, not part of array.
as side note, why ids of horses called _id
? name _id
special field @ root level. embedded documents don't have special fields.
Comments
Post a Comment