"MongoDB Node: Find and Update failing with Dynamic Expression (ObjectId) but working with Manual Input"
rafaeldanda
HOBBYOP

23 days ago

Context:

I'm using n8n (Self-hosted on Railway) to update a MongoDB document. My database uses the standard BSON ObjectId for the _id field.

The Problem: When I manually paste the 24-character string into the "Update Key" field, the node works perfectly and updates the document. However, when I use a dynamic expression to pull the ID from a previous node, the update fails to find the document, even though the resolved value shown in the expression editor is identical to the manual one.

What I've tried:

  1. Using $toObjectId($json._id) to ensure type conversion.

  2. Cleaning the string with .toString().replace(/\"/g, "").trim() to avoid hidden characters or extra quotes.

  3. Testing with relative references ($json._id) and absolute references ($('Node').item.json._id).

  4. Verified that the input node has exactly 1 item to avoid mapping mismatches.

Technical Details:

  • n8n Version: 2.8.3 (Self Hosted)

  • Error Behavior: The node finishes successfully (turns green) but returns "No document found" or simply doesn't update the record in the database.

0 Replies

Loading...