通过子文档中的多个Id查找和更新

时间:2017-12-18 12:18:22

标签: node.js mongodb mongoose

Mongo版本:3.4

我正在使用带有Node的mongoose。我希望通过子文档Id找到并更新子文档。

例如:

{
        "_id" : ObjectId("5a377d62d21a3025a3c3aa49"),
        "id" : "cjbby1a3r0001fnc71nux6a6z",
        "userId" : ObjectId("59e99fc949bae2c07eea8697"),
        "clientId" : ObjectId("59561fa1fbef531e9cfd2206"),
        "isDeleted" : false,
        "updatedAt" : ISODate("2017-12-18T08:33:38.152Z"),
        "createdAt" : ISODate("2017-12-18T08:33:38.152Z"),
        "responses" : [],
        "examples" : [ 
            {
                "_id" : ObjectId("5a377d62d21a3025a3c3aa4d"),
                "isDeleted" : false,
                "updatedAt" : ISODate("2017-12-18T08:33:38.153Z"),
                "createdAt" : ISODate("2017-12-18T08:33:38.153Z"),
                "text" : "how are you"
            }, 
            {
                "_id" : ObjectId("5a377d62d21a3025a3c3aa4c"),
                "isDeleted" : false,
                "updatedAt" : ISODate("2017-12-18T08:33:38.153Z"),
                "createdAt" : ISODate("2017-12-18T08:33:38.153Z"),
                "text" : "hahai"
            }, 
            {
                "_id" : ObjectId("5a377d62d21a3025a3c3aa4b"),
                "isDeleted" : false,
                "updatedAt" : ISODate("2017-12-18T08:33:38.153Z"),
                "createdAt" : ISODate("2017-12-18T08:33:38.153Z"),
                "text" : "hai"
            }, 
            {
                "_id" : ObjectId("5a377d62d21a3025a3c3aa4a"),
                "isDeleted" : false,
                "updatedAt" : ISODate("2017-12-18T08:33:38.152Z"),
                "createdAt" : ISODate("2017-12-18T08:33:38.152Z"),
                "text" : "hahahahahahahahahahaayo"
            }
        ],
    }

查询查询:

 let query = {
    _id: "5a377d62d21a3025a3c3aa49",
    userId: "59e99fc949bae2c07eea8697",
    clientId: "59561fa1fbef531e9cfd2206",
    examples._id = ["5a377d62d21a3025a3c3aa4d", "5a377d62d21a3025a3c3aa4c"]
    isDeleted: false,
};
newExampleQuery = {
    'examples.$.isDeleted': true,
    'examples.$.updatedAt': new Date(),
    'updatedAt': new Date(),
};
Model.update(
    query,
    newExampleQuery

错误:

对于模型“模型”的路径“_id”,对于“['5a377d62d21a3025a3c3aa4d,5a377d62d21a3025a3c3aa4c']的值,对于对象ID,转换为ObjectID

需要找到上述查询并更新5a377d62d21a3025a3c3aa4d,5a377d62d21a3025a3c3aa4c的子文档中的相应数据(isDeleted:true)。

在如何仅通过特定的子集id进行查询的情况下。

0 个答案:

没有答案