williamkapke / mongo-mock

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

Allows operators in find*AndUpdate queries #114

Closed TanukiSharp closed 4 years ago

TanukiSharp commented 4 years ago

Overview

The goal of this PR is to fix issue filled in #113 .

Sorry I'm not really familiar with lodash so I didn't make use of it as I should have done to get the code look more like in accordance with your code style.

I added 5 unit tests that need to run in the correct order (not all of them but at least 2 or 3), the last one cleanup to avoid having the subsequent tests to fail, or changing them to succeed.

Closes #113

TanukiSharp commented 4 years ago

Just to let you know, I found an issue when a property of the condition is the _id, but I'm not sure yet if this is related with my changes. I don't think so but I'm currently investigating to make sure.

Roughly, then I write the following query:

collection.findOneAndUpdate(
    { $and: [{ _id: 123 }, { timestamp: 1 }] },
    { $set: { foo: "alice" } },
    { upsert: true },
    ...
);

The resulting object is as follow:

{
    foo: 'alice',
    _id: ObjectID { id: [Getter] },
    timestamp: 1,
    test: undefined
}

The property _id has been turned into an ObjectID. So the next findOneAndUpdate with the same _id (123) will fail to find a match and insert anew instead of updating.

By the way, I have absolutely no idea where the test property come from...

EDIT 1: If I disable all other tests, the result object doesn't have the test property any longer, the _id is still broken though.

{ foo: 'alice', _id: ObjectID { id: [Getter] }, timestamp: 1 } }

EDIT 2: It turned out the above mentioned problem was already present in mongo-mock, and I've submitted a fix here #115 .

Note that PR #115 depends on #114.

TanukiSharp commented 4 years ago

Any news ? @williamkapke

ronkorving commented 4 years ago

Could we get a release with the recently approved (though not yet merged) PRs?