mongodb - How to properly build a query that filters what I need in mongo -
i tried understand syntax of mongo commands.
i have arrays this:
class="lang-js prettyprint-override">{ "_id" : objectid("5404506df59600af7d285692"), "leagues" : [{ "name" : "nami's marksmen", "tier" : "platinum", "queue" : "ranked_solo_5x5", "entries" : [{ "playerorteamid" : "23712130", "playerorteamname" : "woodyx", "division" : "v", "leaguepoints" : numberlong(56), "wins" : numberlong(83), "ishotstreak" : false, "isveteran" : false, "isfreshblood" : false, "isinactive" : false, "miniseries" : false }], "id" : numberlong(23712130) }, { "name" : "ziggs's redeemers", "tier" : "silver", "queue" : "ranked_team_5x5", "entries" : [{ "playerorteamid" : "team-24f25974-63de-402a-a78f-bb7811f3b362", "playerorteamname" : "ragedeinstall", "division" : "ii", "leaguepoints" : numberlong(58), "wins" : numberlong(7), "ishotstreak" : false, "isveteran" : false, "isfreshblood" : false, "isinactive" : true, "miniseries" : false }], "id" : numberlong(23712130) }, { "name" : "heimerdinger's archons", "tier" : "bronze", "queue" : "ranked_team_3x3", "entries" : [{ "playerorteamid" : "team-b8dbeb70-83b0-11e3-b41c-782bcb497d6f", "playerorteamname" : "h fettarme vollmilch", "division" : "i", "leaguepoints" : numberlong(75), "wins" : numberlong(4), "ishotstreak" : false, "isveteran" : false, "isfreshblood" : false, "isinactive" : true, "miniseries" : false }], "id" : numberlong(23712130) }], "summonerid" : numberlong(23712130), "region" : "euw", "updatedat" : numberlong(1413521614) }
thats first document database. collection contains 100k documents these. these documents want next documents return: leagues.tier : platinum, master, diamond , challenger (there no or involved, and). , leagues.queue : ranked_solo_5x5. documents match query should returned front end end of client (i utilize mongovue). tried utilize didnt work.
class="lang-js prettyprint-override">{ leagues: { $elemmatch: { tier: "platinum", queue: "ranked_solo_5x5"}}}
because returning other 'league.queue'
values too, while should returning platinum , ranked_solo_5x5. new criteria is: leagues.tier : "challenger", "master", "diamond", "platinum"
, leagues.queue : "ranked_solo_5x5"
.
wanted illustration output:
class="lang-js prettyprint-override">{ "_id" : objectid("5404506df59600af7d285692"), "leagues" : [{ "name" : "nami's marksmen", "tier" : "platinum", "queue" : "ranked_solo_5x5", "entries" : [{ "playerorteamid" : "23712130", "playerorteamname" : "woodyx", "division" : "v", "leaguepoints" : numberlong(56), "wins" : numberlong(83), "ishotstreak" : false, "isveteran" : false, "isfreshblood" : false, "isinactive" : false, "miniseries" : false }], "id" : numberlong(23712130) }
to retrieve first matched element within array, can utilize $
positional operator in projection
document of find
query. example:
db.collection.find( // query criteria { leagues: { $elemmatch: { tier: "platinum", queue: "ranked_solo_5x5"}}}, // projection - first matching element within 'leagues' {"leagues.$": 1} )
however, if multiple elements within array satisfy query criteria, above query not help. in such cases, you'll want explore aggregation framework
, provides way de-normalize array using $unwind
operator, handy. example:
db.collection.aggregate([ // split array elements separate documents {"$unwind": "$leagues"}, // specify query criteria {"$match": {"leagues.tier": "platinum", "leagues.queue": "ranked_solo_5x5"}}, // grouping id , reconstruct array {"$group": {"_id": "$_id", "leagues": {"$push":"$leagues"}}} ])
mongodb aggregation-framework
No comments:
Post a Comment