MongoDB can't use $in with ObjectIds

This works for a non ObjectId:

{ "quiz_title":{ "$in":["Quiz 1","Quiz 2" ]}}

As well as this for a single document query:

{ "_id":"63160e3c58f017caa9bb978d"}

However, the following, using $in and multiple ObjectIds does not:

{ "_id":{ "$in":["63160e3c58f017caa9bb978d","63160e3c58f017caa9bb978d" ]}}

This does not work either:

{"_id": {"$in":["ObjectId('63160e3c58f017caa9bb978d')", "ObjectId('63160e3c58f017caa9bb978d')"]}}

Oddly, when using either of the not working examples above in Find, it doesn’t error, but rather it returns as though it did not find any documents. When I switch to Aggregate, both return all the documents.

Am I missing some secret way to format the $in ObjectId query? I’ve searched around, and from what I’ve seen it should work.

I am running n8n with Docker. I am also on 0.191.1 and haven’t gotten around to updating, but I looked through the changelogs and didn’t see any updates that seemed to address this.

Thanks for any help! n8n rocks!

Hey @gravitoad,
I looked into the issue and I can confirm that we currently do not support finding multiple ids at once using the $in operator.

We use the mongodb library where you have to map strings _id = "12345" to objects new ObjectID("12345"). Currently we only do that for the root _id field, as you can see here.

I created an internal ticket to support finding multiple ids supporting nested _id strings. For now you could use the Split In Batches node to find multiple ids. Here is a sample workflow to illustrate.

1 Like