Find in All subkey of Object MongoDB -
i have document in mongodb
{ "name": "test2", "_id": "1502609098801598ffeca615f5d3dd09087c6", "events": { "0": { "delay": "0", "actionid": "2" }, "1": { "delay": "0", "actionid": "3" } } } i want find documents contain event specific actionid tried these can't find want
db.mycollection.find({ "events.$.actionid":"2" }) db.mycollection.find({ "events.$**.actionid":"2" }) db.mycollection.find({ "events.$": { $elemmatch: { "actionid":"2"} }) attention please: can't change document structure , mongodb version 3.0.6
we can take use of aggregation feature within mongodb , project object array
db.test.aggregate( [ { "$project" : { "events" : { "$objecttoarray" : "$events" } } } ] ) after can use normal array filtering $match
db.test.aggregate([ { "$project": { "events": {"$objecttoarray": "$events"} } }, { "$match": { "events.v.actionid" : "2" } } ] ) this output following:
{ "_id" : "1502609098801598ffeca615f5d3dd09087c6", "events" : [ { "k" : "0", "v" : { "delay" : "0", "actionid" : "2" } }, { "k" : "1", "v" : { "delay" : "0", "actionid" : "3" } } ] } > so might want project document orignial structure
Comments
Post a Comment