简体   繁体   English

是否可以在 MongoDb NodeJs 驱动程序中使用 updateMany() 取回受影响的文档 ID?

[英]Is it possible to get back affected documents Ids with updateMany() in MongoDb NodeJs driver?

Right now this is what I get as response from updateMany()现在这就是我从updateMany()得到的响应

Response回复

  [
{
    "result": {
        "n": 1,
        "nModified": 1,
        "opTime": {
            "ts": "6870210161760272385",
            "t": 23
        },
        "electionId": "7fffffff0000000000000017",
        "ok": 1,
        "$clusterTime": {
            "clusterTime": "6870210161760272385",
            "signature": {
                "hash": "nF2DFB6slcL/6QI/GSWOFC5i02I=",
                "keyId": "6808260988001845250"
            }
        },
        "operationTime": "6870210161760272385"
    },
    "connection": {
        "_events": {},
        "_eventsCount": 4,
        "id": 1,
        "address": "40.79.242.150:27017",
        "bson": {},
        "socketTimeout": 360000,
        "monitorCommands": false,
        "closed": false,
        "destroyed": false,
        "lastIsMasterMS": 209
    },
    "modifiedCount": 1,
    "upsertedId": null,
    "upsertedCount": 0,
    "matchedCount": 1,
    "n": 1,
    "nModified": 1,
    "opTime": {
        "ts": "6870210161760272385",
        "t": 23
    },
    "electionId": "7fffffff0000000000000017",
    "ok": 1,
    "$clusterTime": {
        "clusterTime": "6870210161760272385",
        "signature": {
            "hash": "nF2DFB6slcL/6QI/GSWOFC5i02I=",
            "keyId": "6808260988001845250"
        }
    },
    "operationTime": "6870210161760272385"
}
]

This makes no sense to me at all.这对我来说毫无意义。 Except for the following few properties:除了以下几个属性:

"modifiedCount": 0,
"upsertedId": null,
"upsertedCount": 0,
"matchedCount": 1,

Can I get an array of all the ObjectIds or even entire document that have been affected as a result of 'updateMany()' in the response?我是否可以获得所有 ObjectId 甚至整个文档的数组,这些文档因响应中的“updateMany()”而受到影响?

I know something similar is possible with findOneAndUpdate() but the catch is that it updates only a single record.我知道findOneAndUpdate()可以实现类似的功能,但问题是它只更新一条记录。 I don't want to loop over it.我不想遍历它。 That would be bad for performance.这对性能不利。

As far as I know, there isn't a way to get list of updated ids.据我所知,没有办法获取更新的 ID 列表。 Further explanation can be found here (it is based on mongoose but you can extend the reasoning to MongoDb driver) A second query to db should be required, perhaps using updated date field as filter if you know for sure your previous query was the last one (in the first query return you already know how many documents has been updated)可以在此处找到进一步的解释(它基于 mongoose,但您可以将推理扩展到 MongoDb 驱动程序)应该需要对 db 进行第二次查询,如果您确定之前的查询是最后一个,则可能使用更新的日期字段作为过滤器(在第一个查询返回中,您已经知道更新了多少文档)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM