如何更新 mongodb 文档数组中的特定对象项

How to update specific item of object in array of mongdb document

假设我们有以下集合,我对此几乎没有疑问:

{
    "_id" : ObjectId("4faaba123412d654fe83hg876"),
    "user_id" : 123456,
    "total" : 100,
    "items" : [
            {
                    "item_name" : "my_item_one",
                    "price" : 20
            },
            {
                    "item_name" : "my_item_two",
                    "price" : 50
            },
            {
                    "item_name" : "my_item_three",
                    "price" : 30
            }
    ]
}

在这种情况下,我必须一次更新 item_name 或价格,或者同时更新两者。

例如:如果我在 req.body 中提供 item_name,那么它应该只更新 item_name 并且价格保持不变,但如果我提供价格,则反之亦然。

基本上,我的意思是说它应该只更新 req.body 中提供的字段。

我试过了

const result = await User.updateOne(
  {
    user_id: 123456,
    'items.item_name': 'my_item_two',
  },
  { $set: { items: req.body } }
)

但这与我想要的一样,但它正在删除已经存在的剩余字段!

我想你可以在 JS 中创建 $set 对象,然后像这样将它传递给查询:

// body is to mock req.body
const body_1 = {
    item_name: "new_name",
    price: 1
}
const body_2 = {
    item_name: "new_name_2"
}
const body_3 = {
    price: 2
}

const getSetObject = (body) => {
    update = {
        $set: {}
    }
    Object.keys(body).forEach(k => {
        update.$set[`items.$.${k}`] = body[k]
    })
    return update
}

console.log(getSetObject(body_1))
console.log(getSetObject(body_2))
console.log(getSetObject(body_3))

注意这是如何创建这些查询中使用的对象的:

因此您可以创建查询:

const result = await User.updateOne(
  {
    user_id: 123456,
    'items.item_name': 'my_item_two',
  },
  setObj
)