MongoDB 更新数组中的数组对象 [英] MongoDB updating array object within an array

查看:94
本文介绍了MongoDB 更新数组中的数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模型结构:

{ _id: String, calc: [ { preset: String, datasets: [ { _id: String } ] } ] }

API 路由:

router.put('/:gameId/:preset/:datasetId', (req, res) => {
  Game.findOneAndUpdate({ _id: req.params.gameId, 'calc.preset': req.params.preset, 'calc.datasets._id': req.params.datasetId },
    { $set: { 'calc.$.datasets.$': req.body } })
    .then(() => res.status(200).json({ ...req.body, _id: req.params.datasetId }))
    .catch(err => res.status(500).json({ error: err }));
});

如果我要更新 calc 数组中的一个对象,那么我会:$set: { 'calc.$': newCalc },所以我采用了相同的方法来更新一个对象在数据集数组内,即在对象内,即在 calc 数组内,但是, $set: { 'calc.$.datasets.$': newDataset } 不起作用.

If I was to update an object inside calc array then I would: $set: { 'calc.$': newCalc }, so I followed the same approach in order to update an object that is inside datasets array, that is inside an object, that is inside calc array, however, $set: { 'calc.$.datasets.$': newDataset } doesn't work.

如何更新一个带有_id的对象:req.params.datasetId,在datasets数组里面,也就是在calc数组的对象里面,预设是req.params.preset?

How to update an object with _id: req.params.datasetId, inside datasets array, that is inside calc array's object where the preset: req.params.preset?

简而言之: { calc: [ { datasets: [ { set this object to a new dataset } ] } }

推荐答案

你可以使用 $[] positional-all 更新嵌套数组元素,mongo doc for $[]

you can use $[] positional-all to updated nested array elements, mongo doc for $[]

查询

db.t14.update(
   {},
   { $set: { "calc.$[].datasets.$[elem].name": "updated" } },
   { arrayFilters: [  { "elem": "x1" } ], multi: true}
)

带有文档的样本集合

> db.t14.findOne()
{
        "_id" : 1,
        "calc" : [
                {
                        "preset" : "abc",
                        "datasets" : [
                                {
                                        "_id" : "x1",
                                        "name" : "n1"
                                },
                                {
                                        "_id" : "x2",
                                        "name" : "n2"
                                }
                        ]
                }
        ]
}

更新

> db.t14.update({},{$set: { "calc.$[].datasets.$[elem].name": "newname" } },{ arrayFilters: [{ "elem.name": "n1" }], multi: true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

帖子更新

> db.t14.findOne()
{
        "_id" : 1,
        "calc" : [
                {
                        "preset" : "abc",
                        "datasets" : [
                                {
                                        "_id" : "x1",
                                        "name" : "newname"
                                },
                                {
                                        "_id" : "x2",
                                        "name" : "n2"
                                }
                        ]
                }
        ]
}
>

这篇关于MongoDB 更新数组中的数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆