Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在MongoDB中的查询中使用$in中的ObjectId?_Php_Mongodb_Symfony_Doctrine Orm_Doctrine - Fatal编程技术网

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

Php 如何在MongoDB中的查询中使用$in中的ObjectId?,php,mongodb,symfony,doctrine-orm,doctrine,Php,Mongodb,Symfony,Doctrine Orm,Doctrine,我在MongoDB中有以下查询 $this->createQueryBuilder() ->field('actor')->in($actorIdArray) ->getQuery() ->execute(); 其中,field actor是带有注释的对象引用 @MongoDB\ReferenceOne(targetDocument="User", simple=true) 这意味着它将存储

我在MongoDB中有以下查询

$this->createQueryBuilder()
            ->field('actor')->in($actorIdArray)
            ->getQuery()
            ->execute();
其中,field actor是带有注释的对象引用

@MongoDB\ReferenceOne(targetDocument="User", simple=true)
这意味着它将存储对象Id而不是完整引用

当$actorIdArray是具有以下形式的id的数组时

["5706cb39821b166d3931f34f", "56015f7d4f8bd90b769e6e75"]
查询不返回任何内容,这是预期的,因为字段actor包含对象id

但是,如果以这种方式构建阵列

[new MongoId("5706cb39821b166d3931f34f"), new MongoId("56015f7d4f8bd90b769e6e75")]
它也不起作用,这让我很惊讶

日志显示进行了查询

{ "actor": {"$in":[{"$id":"5706cb39821b166d3931f34f"},{"$id":"56015f7d4f8bd90b769e6e75"}]}}
我想应该是这样的

{ "actor": {"$in":[ObjectId("5706cb39821b166d3931f34f"),ObjectId("56015f7d4f8bd90b769e6e75"]}}
不确定我是否做错了什么,
有什么想法吗?

信条希望您的数组是一个文档数组

无需查询即可加载

$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();
$dm=$this->get('doctrine.odm.mongodb.document\u manager'):
$documents=array();
foreach($actorIdArray作为$id){
$documents[]=$dm->getReference('AppBundle:Actor',$id);//createQueryBuilder()
->$documents中的字段('actor')->
->getQuery()
->执行();

正如您可以在
条令\ODM\MongoDB\Query\Builder
->
引用的代码源中看到的那样
方法:

public function references(object $document) : self
    {
        $this->requiresCurrentField();
        $mapping   = $this->getReferenceMapping();
        $reference = $this->dm->createReference($document, $mapping);
        $storeAs   = $mapping['storeAs'] ?? null;
        $keys      = [];

        switch ($storeAs) {
            case ClassMetadata::REFERENCE_STORE_AS_ID:
                $this->query[$mapping['name']] = $reference;

                return $this;
            .....

        return $this;
    }
您必须知道引用是如何生成的。通常,这是通过
$id
生成的

使用
actor.$id
作为字段名:

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

$documents = array();
foreach($actorIdArray as $id){
    $documents[] = new MongoDB\BSON\ObjectId($id);
}

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

它必须是来自ODM的文档数组。如果您想避免对象加载,请使用@Alsatian引用。如果我从控制台{“actor”:{“$in”:[ObjectId(“5706cb39821b166d3931f34f”)、ObjectId(“56015f7d4f8bd90b769e6e75”]}执行该查询,则该查询有效因此,不需要使用文档数组。请序列化我们的数组。当数组包含文档时,它会将它们转换为ObjectId(…)。请尝试。无论如何,我没有文档,因此如果我按照您的建议执行,我需要对$in内的每个id(可以是数百项)进行一次查询。我只需要像在控制台中那样使用条令进行查询(只使用id)。希望@Alsatian能帮助我解决这个问题。它不起作用:(这是$dm->getReference返回的{“initializer”:{},“cloner”:{},“isInitialized”:false}