2016-04-22 56 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。數組本身對所需的操作員沒有影響。