经过对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;
提前致谢
答案 0 :(得分: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;
谢谢。