MongoDB Out Node - Issues with _id on Update

Good Day,

I am attempting to do an update (overwrite) to MongoDB via the mongodb out node.

The notes suggest "... if you want repeated entries to overwrite, then you must set the msg._id property to be a constant by the use of a previous function node."

To test an INSERT, I set msg._id = 1111 in a function node and send that off with the payload and the record is correctly inserted into the db.

I then attempt to do a further INSERT, and get: "MongoError: E11000 duplicate key error collection: tempmdb.userConfig index: id dup key: { : 1111 }".

So far so good, I'm happy the the key is working.

Then I change the mongodb node out operation to UPDATE, and I get: "TypeError: Cannot read property '_id' of undefined".

What am I doing wrong?

Thanks in advance.

1 Like

which of the many mongo nodes are you using?

node-red-node-mongodb

image

I have the same issue. Have you solved? if yes, how?

I ran into this same issue and found your post when trying to solve it. Then I looked at the docs for that node in Node-RED and found this:

Update will modify an existing object or objects. The query to select objects to update uses msg.query and the update to the element uses msg.payload . If msg.query._id is a valid mongo ObjectId string it will be converted to an ObjectId type.

It appears you need to set msg.query._id when using Update but set msg._id when using Save.