2016-04-22 62 views
0

总我有多个数据是这样的撤消放松在MongoDB中

{ 
    "_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 
} 

即时通讯做GROUP BY companyId其做工精细但我想基于matchtimematchId为此我很$unwind matchDatamatchData搜索退卷后,我用我的搜索查询是这样

db.getCollection('matchWiseData').aggregate([ 
{"$match":{ 
    "matchData.matchId":{"$in":[ObjectId("57175c25561d87001e666d12")]} 
}}, 
{"$unwind":"$matchData"}, 
{"$match":{ 
    "matchData.matchId":{"$in":[ObjectId("57175c25561d87001e666d12")]}} 
}]) 

它给我正确的结果,但将退卷后有没有什么办法撤消它我米用放松来只需在子文档内搜索或者在子文档中搜索任何其他方式。

回答

3

那么你当然可以只使用$push$first$group获取文档回到它是:

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" } 
    }} 
]) 

但你可能要在第一刚使用​​用MongoDB的3.2地点:

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") ] } 
        ] 
       } 
      } 
     } 
    }} 
]) 

如果你已经至少MongoDB的2.6,你可能仍然使用$map$setDifference代替:

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] 
      ] 
     } 
    }} 
]) 

这是完全正常的,当每个数组元素已经有一个“独特”的标识,以“套”操作只是从$map删除false值。

这两项的方法来从一个数组的“过滤器”的内容实际上并没有使用$unwind


NB:不知道,如果你真的掌握$in用于匹配的条件“名单“而不是被要求匹配阵列。所以通常条件可以是:

"matchData.matchId": ObjectId("57175c25561d87001e666d12") 

只有实际上只有一个匹配值的地方。当您有一个“列表”条件时,您使用$in$or。数组本身对所需的操作员没有影响。