简体   繁体   中英

mongodb update query in aggregation from multiple collections without grouping

can i update specific fields in a particular collection according to the matched keys from different collections ie suppose i have 3 collections

 **///collection 1: col1///**

    _id:ObjectId("#####7b")
    name:'name1',
    itemsBought:
   [
        {
         "_id":ObjectId("####c1"
          "itemName" : "name1",
          "itemCode" : "IT001",
          "itemQuantity" : 19,
         "itemPrediction":23
         },
        {
         "_id":ObjectId("####c2"
         "itemName" : "name2",
         "itemCode" : "IT002",
         "itemQuantity" : 79,
         "itemPrediction":69
        },
        {
        "_id":ObjectId("####c3"
        "itemName" : "name2",
         "itemCode" : "IT003",
         "itemQuantity" : 0,
         "itemPrediction":0
         },
    ]

    **///collection 1: col2///**

    {
      "itemQuantity" : 21,
      "itemCode" : "IT001",
      },
    {
      "itemQuantity" : 2,
      "itemCode" : "IT003",
      }

    **///collection 1: col3///**

    {
    "itemCode" : "IT001",
    "itemPrediction":23
    },
    {
    "itemCode" : "IT002",
    "itemPrediction":12
    },
    {
    "itemCode" : "IT003",
    "itemPrediction":7
    },

i am using $aggregation $lookup to fetch out all the required data, before sending it to the frontend i need to fetch the values of itemQuantity from col2 and itemPrediction from col3 and update that in col1 with the matching itemCode. So i have the query which fetches out all the data from all the collections but i dont know how to use $set to update the values in col1.

Workaround : You may perform aggregation and save the result manually

db.col1.aggregate([
  {
    $lookup: {
      from: "col2",
      let: {
        root_itemCode: "$itemsBought.itemCode"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $in: [
                "$itemCode",
                "$$root_itemCode"
              ]
            }
          }
        }
      ],
      as: "col2"
    }
  },
  {
    $lookup: {
      from: "col3",
      let: {
        root_itemCode: "$itemsBought.itemCode"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $in: [
                "$itemCode",
                "$$root_itemCode"
              ]
            }
          }
        }
      ],
      as: "col3"
    }
  },
  {
    $addFields: {
      itemsBought: {
        $map: {
          input: "$itemsBought",
          as: "item",
          in: {
            "_id": "$$item._id",
            "itemName": "$$item.itemName",
            "itemCode": "$$item.itemCode",
            "itemQuantity": {
              $let: {
                vars: {
                  input: {
                    $arrayElemAt: [
                      {
                        $filter: {
                          input: "$col2",
                          cond: {
                            $eq: [
                              "$$item.itemCode",
                              "$$this.itemCode"
                            ]
                          }
                        }
                      },
                      0
                    ]
                  },
                  default: "$$item.itemQuantity"
                },
                in: {
                  $ifNull: [
                    "$$input.itemQuantity",
                    "$$default"
                  ]
                }
              }
            },
            "itemPrediction": {
              $let: {
                vars: {
                  input: {
                    $arrayElemAt: [
                      {
                        $filter: {
                          input: "$col3",
                          cond: {
                            $eq: [
                              "$$item.itemCode",
                              "$$this.itemCode"
                            ]
                          }
                        }
                      },
                      0
                    ]
                  },
                  default: "$$item.itemPrediction"
                },
                in: {
                  $ifNull: [
                    "$$input.itemPrediction",
                    "$$default"
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  {
    $unset: [
      "col2",
      "col3"
    ]
  }
])

MongoPlayground

Mongoose

Collection1.aggregate([...], function (err, result) {

    if(err) console.log("error-agg: " + err);

    result.forEach(function(item) {
        Collection1.updateOne({_id:item._id}, {$set:item}, function (err) {
            if(err) console.log("error-saving: " + err);
        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM