williamkapke / mongo-mock

Let's pretend we have a real MongoDB
MIT License
240 stars 75 forks source link

Fix wrong returned _id in updateOne in some cases #115

Closed TanukiSharp closed 4 years ago

TanukiSharp commented 5 years ago

In the case the query look like this:

findOneAndUpdate({ $and: [{ _id: 123 }, { timestamp: 1 }] }, ...

The field id is not obviously apparent in the selector object, hence the following line failing by producing a new ObjectID each time:

cloned._id = selector._id || pk();

(https://github.com/williamkapke/mongo-mock/blob/master/lib/collection.js#L449)

The correct thing to do is to grab _id from the cloned object, because this one is the resolved selector in case of non-obvious queries. It doesn't break existing code, because in case of queries without _id (obvious or not) a new ObjectID is created, falling back to current behavior.

Note: This PR depends on #114.

TanukiSharp commented 5 years ago

Any news ? @williamkapke