Cannot nest $ in $in

I am using the mongodb sync driver v4.2.3 with Spring Boot.

I have a requirement of referencing an array of objects which sit in another collection. We have used DBRef to implement this relationship. Here’s my schema:

Collection1

field1: String
field2: String
field3: Array [ Collection2 DBRef ].

Collection2

field1: String
field2: String

When I run a simple find on Collection1 with a condition of

{field1: value1, field2: value2},

The query above matches one document which has 2 elements inside field3 (an Array). I can see that the Java driver is automatically trying to fetch the 2 documents from Collection2 ALSO which are part of the document in Collection1. It runs a find query with a condition like this:

{"_id": {"$in": [{"$oid": "6274f6cdb114ec8177ff48a6"}, {"$oid": "6274f6e3b114ec8177ff48a7"}]}

And that throws an exception:

com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): ‘cannot nest $ under $in’ on server testtimeseries-shard-00-01.35m0c.mongodb.net:27017. The full response is {“ok”: 0.0, “errmsg”: “cannot nest $ under $in”, “code”: 2, “codeName”: “BadValue”, “$clusterTime”: {“clusterTime”: {"$timestamp": {“t”: 1651934469, “i”: 1}}, “signature”: {“hash”: {"$binary": {“base64”: “jolmdp4RkFGQXXr08b+10Lh9x9Q=”, “subType”: “00”}}, “keyId”: 7083736286242013188}}, “operationTime”: {"$timestamp": {“t”: 1651934469, “i”: 1}}}

When I run the same query on Collection2 manually from the mongo shell, like this:

{"_id": {"$in": [ObjectId("6274f6cdb114ec8177ff48a6"), ObjectId("6274f6e3b114ec8177ff48a7")]}}

it works absolutely fine and returns the two documents.

I do have a definition of a Repository object for Collection1 as follows, but I don’t have anything for Collection2:

public interface Collection1Repository extends MongoRepository<Collection1, String>

Is there something different I need to be doing in order to make this work? Is this an unsupported scenario, or a bug that has been fixed in a later version?
Do I need to define a Repository class and some find operations on it? I just have a POJO with @Document(collection = “Collection2”) annotation for it.

Do in java the same thing that you do in javascript via mongosh.

The class is ObjectId (mongo-java-driver 3.6.0 API).

In JS you did:

{"$in": [ObjectId("6274f6cdb114ec8177ff48a6"), ObjectId("6274f6e3b114ec8177ff48a7")]}

rather than

{"$in": [{"$oid": "6274f6cdb114ec8177ff48a6"}, {"$oid": "6274f6e3b114ec8177ff48a7"}]}

In java you do:

{"$in": [new ObjectId("6274f6cdb114ec8177ff48a6"), new ObjectId("6274f6e3b114ec8177ff48a7")]}

I’m not manually running the second query. The java driver is automatically trying to fetch from collection2 using $oid instead of ObjectId inside a $in, and I have no control over that.

If I was running that query, I would have made that change you suggested, I agree.

Is there something else I need to do in Collection2.java?