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

Viewed 1561

Right now this is what I get as response from 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?

I know something similar is possible with findOneAndUpdate() but the catch is that it updates only a single record. I don't want to loop over it. That would be bad for performance.

1 Answers

As far as I know, there isn't a way to get list of updated ids. 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)

Related