node.js - Undo Unwind in aggregate in mongodb -
i have multiple data this
{ "_id" : objectid("57189fcd72b6e0480ed7a0a9"), "venueid" : objectid("56ce9ead08daba400d14edc9"), "companyid" : objectid("56e7d62ecc0b8fc812b2aac5"), "cardtypeid" : objectid("56cea8acd82cd11004ee67a9"), "matchdata" : [ { "matchid" : objectid("57175c25561d87001e666d12"), "matchdate" : isodate("2016-04-08t18:30:00.000z"), "matchtime" : "20:00:00", "_id" : objectid("57189fcd72b6e0480ed7a0ab"), "active" : 3, "cancelled" : 0, "produced" : 3 }, { "matchid" : objectid("57175c25561d87001e666d13"), "matchdate" : isodate("2016-04-09t18:30:00.000z"), "matchtime" : "20:00:00", "_id" : objectid("57189fcd72b6e0480ed7a0aa"), "active" : null, "cancelled" : null, "produced" : null } ], "__v" : 0 }
i m doing group companyid
, work fine want search in matchdata
based on matchtime
, matchid
purpose $unwind matchdata
after unwind using search query this
db.getcollection('matchwisedata').aggregate([ {"$match":{ "matchdata.matchid":{"$in":[objectid("57175c25561d87001e666d12")]} }}, {"$unwind":"$matchdata"}, {"$match":{ "matchdata.matchid":{"$in":[objectid("57175c25561d87001e666d12")]}} }])
its give me proper result after applying unwind there way undo m using unwind search inside subdocument or there other way search inside subdocument.
well can of course use $push
, $first
in $group
document was:
db.getcollection('matchwisedata').aggregate([ { "$match":{ "matchdata.matchid":{"$in":[objectid("57175c25561d87001e666d12")]} }}, { "$unwind":"$matchdata"}, { "$match":{ "matchdata.matchid":{"$in":[objectid("57175c25561d87001e666d12")]} }}, { "$group": { "_id": "$_id", "venueid": { "$first": "$venueid" }, "companyid": { "$first": "$companyid" }, "cardtypeid": { "$first": "$cardtypeid" }, "matchdata": { "$push": "$matchdata" } }} ])
but should have used $filter
mongodb 3.2 in first place:
db.getcollection('matchwisedata').aggregate([ { "$match":{ "matchdata.matchid":{"$in":[objectid("57175c25561d87001e666d12")]} }}, { "$project": { "venueid": 1, "companyid": 1, "cardtypeid": 1, "matchdata": { "$filter": { "input": "$matchdata", "as": "match", "cond": { "$or": [ { "$eq": [ "$$match.matchid", objectid("57175c25561d87001e666d12") ] } ] } } } }} ])
and if had @ least mongodb 2.6, still have used $map
, $setdifference
instead:
db.getcollection('matchwisedata').aggregate([ { "$match":{ "matchdata.matchid":{"$in":[objectid("57175c25561d87001e666d12")]} }}, { "$project": { "venueid": 1, "companyid": 1, "cardtypeid": 1, "matchdata": { "$setdifference": [ { "$map": { "input": "$matchdata", "as": "match", "in": { "$cond": [ { "$or": [ { "$eq": [ "$$match.matchid", objectid("57175c25561d87001e666d12") ] } ]}, "$$match", false ] } }}, [false] ] } }} ])
that's fine when every array element has "unique" identifier, "set" operation removes false
values $map
.
both of ways "filter" content array without using $unwind
n.b: not sure if grasp $in
used match "list of conditions" rather being required match on arrays. condition can be:
"matchdata.matchid": objectid("57175c25561d87001e666d12")
where have single value match on. use $in
, $or
when have "list" of conditions. arrays make no difference operator required.
Comments
Post a Comment