MongoDB update field value as ObjectId

Hello, using MongoDB am trying to update field value as ObjectId, but it is updating as string as follows and the table relation is failing.

{ 
   "userName":"adam",
   "roleId": "ObjectId("603ca6cd58f2c71d50bd4cb0")"
}

Could some one please help me the update the value as follows

{ 
   "userName":"adam",
   "roleId": ObjectId("603ca6cd58f2c71d50bd4cb0")
}

Thanks in advance…

Yes, this is currently not possible. Currently, ObjectID is just supported when updating the collection’s ID. Will add it to my to-do list.

1 Like

Like I said in other question about date support, In the MongoDB documentation there is support for Extended JSON - Node driver should support this already. And so, a ObjectId could be represented by:

{"$oid":"5d505646cf6d4fe581014ab2"}
1 Like

Interesting. This is quite helpful. We would probably have to make a breaking change in the node but still a good idea. Would you please make a feature request for this and upvote it?

Done MongoDB Node: Support for Extended JSON

1 Like

workaround:

{ "$expr": { "$eq": ["$_id", { "$toObjectId": "<object-id>" }] } }
2 Likes

@kimus How does your workaround work in the n8n node? I need this funtionality like, so bad. I can’t understand how it’s not included, it’s a super common uscase.

@RicardoE105 I ran accross the need for this feature this week so I created a PR that adds the option for it. It behaves the same way as the Date Fields. You pass a comma-delimited list of fields that you want converted to ObjectID before running the following operations: insert, update, findAndUpdate, findAndReplace. You think you can take a look and review?

1 Like

Hey @dejecj,

Welcome to the community :cake:

I spotted the PR this morning, It will need to go through our normal PR review process. At first glance it looks good though.

Ok got it. Just wasn’t sure what your normal review process looks like. I tried to find info about in he docs but no luck, At least not in the CONTRIBUTING.md. I just know the github actions are pending approval. Is this review process described somewhere so I can read up?

1 Like

Hey @dejecj,

The review process isn’t documented anywhere as while we would love to say we would review it within a week or 2 things can happen that would shift the priority so we tend to pop a message on the PR when we are about to start unless it is something really quick like fixing a typo.

it does. I’m using it in my one flows.

let’s say you want to do a query from a id.

  1. Choose the Find operation
  2. Choose the Collection you want to get data from
  3. and write the expression like:
{ "$expr": { "$eq": ["$_id", { "$toObjectId": "{{$json["someId"]}}" }] } }

this expression will convert a string to a ObjectId.

@gravitoad do you need something different? Just let me know.

1 Like

Hey @kimus, I didn’t get notified of your response. Do you have an example of adding a related ObjectId to a document, like the original poster’s question?

@gravitoad I guess I should be more cleared on my reply.

I’ve used tthe $expr and $toObjectId to query the database and not for inserting or updating.
I did not tested or know how to use this in n8n MongoDB’s node. That seams to lack more advanced features.

Sorry about not clearing this out in first time.

Btw, In mongo you can convert string to ObjectId that by doing:

db.Test.insertOne({ userName: 'adam' });
db.Test.update(
    { userName: 'adam' }, 
    [
        { $addFields: {
            roleId: { $toObjectId: '639664ff6e4d62feb6d0b1da' }
        }}
    ]
);