如何在MongoDB中查找和更新文档

a8jjtwal  于 2023-02-03  发布在  Go
关注(0)|答案(1)|浏览(137)

我也有类似的收藏

db={
  collectionA: [
    {
      "id": ObjectId("63b7c24c06ebe7a8fd11777b"),
      "uniqueRefId": "UUID-2023-0001",
      "products": [
        {
          "productIndex": 1,
          "isProdApproved": false,
          "productCategory": ObjectId("63b7c24c06ebe7a8fd11777b"),
          "productOwners": [
            {
              _id: ObjectId("63b7c2fd06ebe7a8fd117781"),
              iApproved: false
            },
            {
              _id: ObjectId("63b7c2fd06ebe7a8fd117782"),
              iApproved: false
            }
          ]
        },
        {
          "productIndex": 2,
          "isProdApproved": false,
          "productCategory": ObjectId("63b7c24c06ebe7a8fd11777b"),
          "productOwners": [
             {
              _id: ObjectId("63b7c2fd06ebe7a8fd117781"),
              iApproved: false
            },
            {
              _id: ObjectId("63b7c2fd06ebe7a8fd117783"),
              iApproved: false
            }
          ]
        },
        {
          "productIndex": 3,
          "productCategory": "",
          "productOwners": ""
        }
      ]
    }
  ]
}

我想在产品所有者中查找**_id63b7c2fd06ebe7a8fd117781产品所有者**,并将isApprovedisprodApproved更新为true。其他数据将保持不变。
我已经尝试过这个,但它只是更新第一次发生

db.collectionA.update(
  {
    _id: ObjectId('63b7c24c06ebe7a8fd11777b'),
    'products.productOwners._id': ObjectId('63b7c2fd06ebe7a8fd117781'),
  },
  { $set: { 'products.$.productOwners.$[x].isApproved': true } },
  { arrayFilters: [{ 'x._id': ObjectId('63b7c2fd06ebe7a8fd117781') }] }
);
83qze16e

83qze16e1#

这一条应该行得通:

db.collection.updateMany({},
   [
      {
         $set: {
            products: {
               $map: {
                  input: "$products",
                  as: "product",
                  in: {
                     $cond: {
                        if: { $eq: [{ $type: "$$product.productOwners" }, "array"] },
                        then: {
                           $mergeObjects: [
                              "$$product",
                              { isProdApproved: { $in: [ObjectId("63b7c2fd06ebe7a8fd117781"), "$$product.productOwners._id"] } },
                              {
                                 productOwners: {
                                    $map: {
                                       input: "$$product.productOwners",
                                       as: 'owner',
                                       in: {
                                          $mergeObjects: [
                                             "$$owner",
                                             { iApproved: { $eq: ["$$owner._id", ObjectId("63b7c2fd06ebe7a8fd117781")] } }
                                          ]
                                       }
                                    }
                                 }
                              }
                           ]
                        },
                        else: "$$product"
                     }
                  }
               }
            }
         }
      }
   ]
)

但是,数据似乎是多余的,最好只更新products.productOwners.iApproved,然后从嵌套元素派生products.isProdApproved

db.collection.aggregate([
   {
      $set: {
         products: {
            $map: {
               input: "$products",
               as: "product",
               in: {
                  $cond: {
                     if: { $eq: [{ $type: "$$product.productOwners" }, "array"] },
                     then: {
                        $mergeObjects: [
                           "$$product",
                           { isProdApproved: { $anyElementTrue: ["$$product.productOwners.iApproved"] } },
                        ]
                     },
                     else: "$$product"
                  }
               }
            }
         }
      }
   }
])

相关问题