0

经过对 mongodb 的 createquerybuilder 的几次研究,我无法解决我的问题。你能帮帮我吗?这是数据的结构:

“Field1” : { “Field2”: “value1a”,“Field3” : {“Field4” : “value2a”,”Field5” : “value3a”}}
“Field1” : { “Field2”: “value1b”,“Field3” : {“Field4” : “value2b”,”Field5” : “value3b”}}
“Field1” : { “Field2”: “value1c”,“Field3” : {“Field4” : “value2c”,”Field5” : “value3c”}}

如何获得 Field5 = value3a 的整行?我继续使用下面的代码,但它没有返回任何记录:

$keywords = "test";
$keywords_Array = explode(',',$keywords);
$nbKeyword = count($keywords_Array);
$odm = $this->get("doctrine mongodb");
$donnees =  $odm->createQueryBuilder("MyBundle:MyCollection");
for($i=0; $i<$nbKeyword; $i++){
$search = new \MongoRegex('/"Field5":"'.$ keywords_Array [$i].'"/');
$donnees = $donnees->field("Field1")->equals(new \MongoRegex('/"Field3":"'.$search.'"/'));
}
$donnees = $donnees->getQuery()->execute();
Foreach($donnees  as $ data)
{
$resp = $data->getResult();
}
Return $resp;

还有另一种数据结构(这里,数据是一个值数组):

“Field1” : {“Field2”: [“value1”,”value2”,”value3”]}

如何获得 Field2 = value3 的整行?它与上面的代码相同,它不返回任何记录:

 $keywords = "test";
 $keywords_Array = explode(',',$keywords);
 $nbKeyword = count($keywords_Array);
 $odm = $this->get("doctrine mongodb");
 $donnees =  $odm->createQueryBuilder("MyBundle:MyCollection");
 for($i=0; $i<$nbKeyword; $i++){                     
     $donnees = $donnees-> field("Field1")->equals(new \MongoRegex('/"Field2":"'. $keywords_Array [$i].'"/i'));
            }
 $donnees = $donnees->getQuery()->execute();
 Foreach($donnees  as $ data)
 {
$resp = $data->getResult();
 }
 Return $resp;

提前致谢

4

1 回答 1

0

问题已解决。我发布解决方案以防万一有人也遇到同样的问题。对于第一个数据结构,代码应该是这样的:

$keywords = "test";
$keywords_Array = explode(',',$keywords);
$nbKeyword = count($keywords_Array);
$odm = $this->get("doctrine mongodb");
$donnees =  $odm->createQueryBuilder("MyBundle:MyCollection");
for($i=0; $i<$nbKeyword; $i++){
$donnees = $donnees->field('Field1')->equals(new \MongoRegex('/"Field5":'.$keywords_Array [$i].'/'));
}
$donnees = $donnees->getQuery()->execute();
Foreach($donnees  as $ data)
{
    $resp = $data->getResult();
}
Return $resp;

而对于第二种数据结构,代码应该是这样的:

 $keywords = "test";
 $keywords_Array = explode(',',$keywords);
 $nbKeyword = count($keywords_Array);
 $odm = $this->get("doctrine mongodb");
 $donnees =  $odm->createQueryBuilder("MyBundle:MyCollection");
 for($i=0; $i<$nbKeyword; $i++){       
$keys = new \MongoRegex("/".$keywords_Array [$i]."/");
    $donnees = $donnees->field("Field1")->in(array("Field2"=>$keys));
 }
 $donnees = $donnees->getQuery()->execute();
 Foreach($donnees  as $ data)
 {
    $resp = $data->getResult();
 }
 Return $resp;

谢谢你。

于 2013-06-26T12:33:05.917 回答