Mongo delete document with _id

Hello,

I’m trying to delete a document of a collection with its ObjectId ( _id) but I don’t know exactly what I’m doing wrong:

My record in mongo express is:

and my node is:

how should I write the Json ? I tried many different things but none is working.

Thanks for your support

Hey @Jeremy_controlc.io,

I am not really up to speed with mongo but looking at the docs maybe something like the below will work.

{"_id": ObjectId("your-id")}

Hello,
Thanks for the quick reply, the problem is that the node is expected a JSON format and this isn’t working.
I tried differrent stuff found on the net but nothing is working.
The alternative I found is to save the builtin id into another “field” and filter on that one to delete. This workaround is working but of course I would prefer to directly delete with the builtin id . I also guess that it’s technically more efficient to do so.

Yes, we really have to improve this! In the meantime you can fine a workaround mentioned here:

So it would be with a fixed ID:

{ "$expr": { "$eq": ["$_id", { "$toObjectId": "your-id" }] } }

or with expression:

{ "$expr": { "$eq": ["$_id", { "$toObjectId": "{{$json["_id"]}}" }] } }
2 Likes

Hello @jan,
That’s exactly what I needed. I guess I need to learn more about Mongo expressions…
Vielen danke !

1 Like

Glad to hear that it helped! Honestly have no idea about Mongo either, really just copied it from the linked answer above, so all the praise should go to @kimus.

Have fun!

1 Like