Illegal ObjectId : argument must be a 24 character hexadecimal
I tried to look at my variable by changing ObjectId(“$$id”) with “$$id” and everything looks good. I get my field “convert_NID” with my 24 character string for each document.
The problem here is that ObjectId() is a client side method (i.e. mongo shell) and not an aggregation operator/method that is able to resolve the $$id reference on the aggregation pipeline execution.
If you’re MongoDB server version is v4.0+, you could utilise $toObjectId aggregation pipeline operator to convert 24-long hexadecimal string to an ObjectId.
Thank you for your answer, it’s great.
I was about to select your reply as the solution but perhaps I can ask for more help if I may.
The company for which I work for is working on MongoDB 3.6, so I don’t have access to your operator, and our database won’t be upgraded soon. For the meantime, is there another solution ?
Depending on the use case, you could utilise any of MongoDB drivers to write a client side script to either update the field to ObjectId() format or to add a new field that contains the ObjectId() value.