MongoDB findOneAndUpdate returnDocument not working

Viewed 25

I have following code that returns findOneAndUpdate response. But returnDocument: "after" not working properly when I'm using it with arrayFilters. If I remove arrayFilters it works fine. What causes this or is there a better way to get modified document in response?

const addStartingCards = async (lobbyId) => {
    const res = await db.findOneAndUpdate(
        { lobbyId: lobbyId },
        {
            $push: {
                "seats.$[el].cards": {
                    $each: [Math.floor(Math.random() * 52), Math.floor(Math.random() * 52)]
                }
            }
        },
        {
            arrayFilters: [
                {
                    "el.status": true,
                }
            ]
        },
        { returnOriginal: false, returnDocument: "after" });
    return (res.value);
};
1 Answers

As @rickhg12hs suggested in comments, this is the correct format to use them together;

{arrayFilters: [{"el.status": true}], returnDocument: "after" }
Related