MongoError: Unrecognized pipeline stage name: '$push

Viewed 1266

I need to use both $set and $push within one update call. I have done it as mentioned in $push and $set in same MongoDB update

Now my code is

await this.collection.updateOne(
            { id },
            [
                {
                    $set: {
                        ...changes,
                        "metadata.updated": new Date(),
                        "metadata.updatedBy": { id: updatedBy }
                    }
                },
                {
                    $push: {
                        history: {
                            user: { id: updatedBy },
                            type: HistoryType.TIME_ENTRY_UPDATED,
                            date: new Date(),
                            args: changes
                        }
                    }
                }
            ]
        );

But it is throwing MongoError: Unrecognized pipeline stage name: '$push' Whats wrong with my code.

My mongodb server version - 4.2.9 node package client version - ^2.2.36

1 Answers

I'm using mongodb node native driver, and My installed mongodb version is v4.4.5, and my node version is v14.15.4

I faced this problem too and I change update body command to below and it's worked for me.

 await this.collection.updateOne(
        { id },
        {
                $set: {
                    ...changes,
                    "metadata.updated": new Date(),
                    "metadata.updatedBy": { id: updatedBy }
                },                
                $push: {
                    history: {
                        user: { id: updatedBy },
                        type: HistoryType.TIME_ENTRY_UPDATED,
                        date: new Date(),
                        args: changes
                    }
                }
         }
    );
Related