Javascript 这是findOneAndUpdate的$operator的正确用法吗?

Javascript 这是findOneAndUpdate的$operator的正确用法吗?,javascript,mongodb,Javascript,Mongodb,我想更新mongo文档中的一个值,它是数组的数组 这是我到目前为止的代码。。。但它不起作用。我想我需要使用图表上的$运算符来访问当前选定的人员。。。但不知道这是怎么回事 我真的在为mongoDB语法和更新数组而挣扎 这是我的数据集,我在各自的阵列中放入了t1、t2和t3 [ { "_id": "5e3d891d956bb31c307d8146", "t1": 0, "affiliation": "800_800", "year": 2020, "mon

我想更新mongo文档中的一个值,它是数组的数组

这是我到目前为止的代码。。。但它不起作用。我想我需要使用图表上的$运算符来访问当前选定的人员。。。但不知道这是怎么回事

我真的在为mongoDB语法和更新数组而挣扎

这是我的数据集,我在各自的阵列中放入了t1、t2和t3

[
  {
    "_id": "5e3d891d956bb31c307d8146",
    "t1": 0,
    "affiliation": "800_800",
    "year": 2020,
    "month": "February",
    "weekNumber": 6,
    "weekStart": "02/02/2020",
    "weekEnd": "02/08/2020",
    "chart": [
      {
        "t2": 0,
        "_id": "5e3d8a6358a3d92448e85aa5",
        "ordinal": 5,
        "ordinalString": "Friday",
        "chorePerson": [
          {
            "completed": false,
            "t3": 0,
            "_id": "5e3d8a6358a3d92448e85aa7",
            "person": "Frank",
            "personID": "5e2891587678601434f6929c",
            "phone": "8008008002",
            "chore": "Another one",
            "choreID": "5e39ca3949acee16fc280c98"
          }
        ]
      }
    ],
    "date": "2020-02-07T16:03:47.770Z",
    "__v": 0
  }
]
我已经能够使用以下查询更新t1和t2:

{"_id":"5e3d891d956bb31c307d8146","chart._id":"5e3d9260fc365c2d080a32ce"}

还有一套电话

{"$set":{"t1":1,"chart.$.t2":2}}
但我不知道如何在数组中再下一级

我的问题是:我硬编码了“图表[1]”,试图强制它只获取该记录

{"_id":"5e3d891d956bb31c307d8146","chart._id":"5e3d8a6358a3d92448e85aa5","chart[1].chorePerson._id":"5e3d8a6358a3d92448e85aa7"}
这是我的“设定”电话

{"$set":{"t1":1,"chart.$.t2":2,"chart.$[].chorePerson.$.t3":3}}
当我在程序中运行此命令时,我得到:

error Updating the path 'chart.$[].chorePerson.$.t3' would create a conflict at 'chart'
更新1

所以我试了一下:

{"_id":"5e3d93777f099b28b0fff2ae","chart._id":"5e3d953ed92a082738e8e2b9","chart.chorePerson._id":"5e3d953ed92a082738e8e2bb"}

{"$set":{"t1":1,"chart.$.t2":2,"chart.$.chorePerson.$.t3":3}}

这给了我:

error Too many positional (i.e. '$') elements found in path 'chart.$.chorePerson.$.t3
error Updating the path 'chart.$[].chorePerson.$.t3' would create a conflict at 'chart'
所以我想我会回到whoami发布的答案:

{"$set":{"t1":1,"chart.$.t2":2,"chart.$[].chorePerson.$.t3":3}}
这给了我:

error Too many positional (i.e. '$') elements found in path 'chart.$.chorePerson.$.t3
error Updating the path 'chart.$[].chorePerson.$.t3' would create a conflict at 'chart'
更新2

因此,我尝试将set语句中的[]移动到图表中。$.chorePerson

{"$set":{"t1":1,"chart.$.t2":2,"chart.$.chorePerson.$[].t3":3}}
并仅选择相关人员5E3DA771E08E3E3E31AC420004

{"_id":"5e3da771e08e3e31ac41fffd","chart._id":"5e3da771e08e3e31ac420002","chart.chorePerson._id":"5e3da771e08e3e31ac420004"}
这让我更接近。。。现在,数据被设置在正确的杂务图表和图表中,但是所有的杂务人员字段都在更新,即使我的选择应该只返回杂务人员“5e3da771e08e3e31ac420004”


经过反复试验和大量关于堆栈溢出的文章,我终于找到了我需要的代码:

                var query = {"chart.chorePerson._id":idCP};

                var update = {$set: {"chart.$.chorePerson.$[elem].completed":true, "chart.$.chorePerson.$[elem].completed_at": Date.now()}};

                var options = {new: true, arrayFilters: [ { "elem._id": { $eq: idCP } } ]};

                if(debugThis){
                    console.log("query " + JSON.stringify(query));

                    console.log("update " + JSON.stringify(update));

                    console.log("options " + JSON.stringify(options));
                }

                // see stackoverflow question:
                // https://stackoverflow.com/questions/60099719/is-this-the-correct-use-of-the-operator-for-findoneandupdate/60120688#60120688

                ChoreChart.findOneAndUpdate( query, update, options)
                    .then(chart => {

                        if(debugThis){
                            console.log('updated chart ' + JSON.stringify(chart));
                        }

                        return resolve(msgResponse);
                })
                .catch(err => {
                    msgResponse.message('error ' + err.message);
                    return resolve(msgResponse);
                })
这正是我想要的:

非常感谢写这些文章的人:


-我感谢你在这方面的帮助。。。我已经用ID信息更新了问题Nice al d bst!!-你让我开始思考[]解决方案的思路。。。所以也要感谢你!