在没有$ unwind

时间:2018-11-04 06:49:35

标签: node.js mongodb mongoose mongodb-query aggregation-framework

我有以下workouts个收藏

[
  {
    "receiverWorkout": {
      "exercises": [
        { "_id": "1", "reps": 0 },
        { "_id": "2", "reps": 4 }
      ]
    },
    "senderWorkout": {
      "exercises": [
        { "_id": "2", "reps": 12 },
        { "_id": "1", "reps": 8 }
      ]
    }
  }
]

我需要从_idreceiverWorkout练习中匹配相同的senderWorkout,并想添加键won true false reps更大。

如果您看到receiverWorkout个练习({ "_id": "1", "reps": 0 })的第一个元素与senderWorkout个练习({ "_id": "1", "reps": 8 })的第二个元素匹配,因为两者具有相同的{{ 1}}

预期输出应为

_id

我想避免使用[ { "receiverWorkout": { "exercises": [ { "_id": "1", "reps": 0, "won": false }, { "_id": "2", "reps": 4, "won": false } ] }, "senderWorkout": { "exercises": [ { "_id": "2", "reps": 12, "won": true }, { "_id": "1", "reps": 8, "won": true } ] } } ]

请帮助!

2 个答案:

答案 0 :(得分:1)

您不需要使用数组索引来实现此目的。如果我能很好地理解,您需要知道senderWorkout.exercises数组中的每个条目,是否在ReceiverWorkout.exercises数组中存在具有相同_id的元素。这是执行此操作的方法:

db.collection.aggregate([
  {
    $addFields: {
      "senderWorkout.exercises": {
        $map: {
          input: "$senderWorkout.exercises",
          as: "ex",
          in: {
            $cond: {
              if: {
                $in: [
                  "$$ex._id",
                  "$receiverWorkout.exercises._id"
                ]
              },
              then: {
                _id: "$$ex._id",
                reps: "$$ex.reps",
                result: true
              },
              else: {
                _id: "$$ex._id",
                reps: "$$ex.reps",
                result: false
              }
            }
          }
        }
      }
    }
  }
])

您可以尝试here

答案 1 :(得分:1)

您可以使用以下汇总。

单次通行证

db.colname.aggregate([
 {"$project":{
  "exercises":{
   "$reduce":{
    "input":"$receiverWorkout.exercises",
    "initialValue":{"receiverWorkout":[],"senderWorkout":[]},
    "in":{"$let":{
     "vars":{
      "sen":{"$arrayElemAt":["$senderWorkout.exercises",{"$indexOfArray":["$senderWorkout.exercises._id","$$this._id"]}]}
     },
     "in":{
      "receiverWorkout":{
       "$concatArrays":[
        "$$value.receiverWorkout",
        [{"_id":"$$this._id","reps":"$$this.reps","won":{"$gt":["$$this.reps","$$sen.reps"]}}]
       ]
      },
      "senderWorkout":{
       "$concatArrays":[
        "$$value.senderWorkout",
        [{"_id":"$$sen._id","reps":"$$sen.reps","won":{"$gt":["$$sen.reps","$$this.reps"]}}]
       ]
      }
     }
    }}
   }
  }
 }},
 {"$project":{
  "receiverWorkout.exercises":"$exercises.receiverWorkout",
  "senderWorkout.exercises":"$exercises.senderWorkout"
 }}
])

不使用$unwind

db.colname.aggregate(
{"$project":{
  "receiverWorkout.exercises":{
    "$map":{
      "input":"$receiverWorkout.exercises",
      "in":{
        "$let":{
          "vars":{"sen":{"$arrayElemAt":["$senderWorkout.exercises",{"$indexOfArray":["$senderWorkout.exercises._id","$$this._id"]}]}},
          "in":{
            "_id":"$$this._id",
            "reps":"$$this.reps",
            "won":{"$gt":["$$this.reps","$$sen.reps"]}
          }
        }
      }
    }
  },
  "senderWorkout.exercises":{
    "$map":{
      "input":"$senderWorkout.exercises",
      "in":{
        "$let":{
          "vars":{"rec":{"$arrayElemAt":["$receiverWorkout.exercises",{"$indexOfArray":["$receiverWorkout.exercises._id","$$this._id"]}]}},
          "in":{
            "_id":"$$this._id",
            "reps":"$$this.reps",
            "won":{"$gt":["$$this.reps","$$rec.reps"]}
          }
        }
      }
    }
  }
}})

使用$unwind

db.colname.aggregate([
  {"$unwind":"$senderWorkout.exercises"},
  {"$unwind":"$receiverWorkout.exercises"},
  {"$sort":{"receiverWorkout.exercises._id":1,"senderWorkout.exercises._id":1}},
  {"$addFields":{
    "receiverWorkout.exercises.won":{
      "$cond":[
        {"$gt":["$receiverWorkout.exercises.reps","$senderWorkout.exercises.reps"]},
        true,
        false
      ]
    },
    "senderWorkout.exercises.won":{
      "$cond":[
        {"$gt":["$receiverWorkout.exercises.reps","$senderWorkout.exercises.reps"]},
        false,
        true
      ]
    }
  }},
  {"$group":{
    "_id":"$_id",
    "receiverWorkout":{"$addToSet":"$receiverWorkout.exercises"},
    "senderWorkout":{"$addToSet":"$senderWorkout.exercises"}
  }}
])