如何在MongoDB中使用$ in查询中的ObjectId?

I have the following query in MongoDB

$this->createQueryBuilder()
            ->field('actor')->in($actorIdArray)
            ->getQuery()
            ->execute();

where the field actor is an object reference with annotation

@MongoDB\ReferenceOne(targetDocument="User", simple=true)

which means it will store the object Id instead of the full reference.

When $actorIdArray is an array of id with the form

["5706cb39821b166d3931f34f", "56015f7d4f8bd90b769e6e75"]

the query does not return nothing, which is the expected since the filed actor contains object id.

However, if I build the array this way

[new MongoId("5706cb39821b166d3931f34f"), new MongoId("56015f7d4f8bd90b769e6e75")]

it doesn't work either, which is quite surprising for me.

The log shows the query is made

{ "actor": {"$in":[{"$id":"5706cb39821b166d3931f34f"},{"$id":"56015f7d4f8bd90b769e6e75"}]}}

and I think it should be something like this

{ "actor": {"$in":[ObjectId("5706cb39821b166d3931f34f"),ObjectId("56015f7d4f8bd90b769e6e75"]}}

Not sure if I am doing something wrong, any ideas?

Doctrine wants your array to be an array of documents.

You can load document references without query.

$dm = $this->get('doctrine.odm.mongodb.document_manager'):

$documents = array();
foreach($actorIdArray as $id){
    $documents[] = $dm->getReference('AppBundle:Actor',$id); // <- this is the key
}

$this->createQueryBuilder()
            ->field('actor')->in($documents)
            ->getQuery()
            ->execute();