Php ODM/MongoDB条令:在查询中和在查询中
我在mongoDB中使用Doctrine ODM 我尝试进行如下查询:Php ODM/MongoDB条令:在查询中和在查询中,php,mongodb,doctrine,doctrine-orm,doctrine-odm,Php,Mongodb,Doctrine,Doctrine Orm,Doctrine Odm,我在mongoDB中使用Doctrine ODM 我尝试进行如下查询: $queryBuilder->field('array_field')->in('even_value_1', 'event_value_2'); $queryBuilder->field('array_field')->in('odd_value_1', 'odd_value_2'); 这样做的目的是选择在其文档中包含的所有文档 array_field(event_value_1 OR event
$queryBuilder->field('array_field')->in('even_value_1', 'event_value_2');
$queryBuilder->field('array_field')->in('odd_value_1', 'odd_value_2');
这样做的目的是选择在其文档中包含的所有文档
array_field(event_value_1 OR event_value_2) AND (odd_value_1 OR odd_value_2)
通过我使用的语法,我得到的文档
event_value_1 OR event_value_2 OR odd_value_1 OR odd_value_2
关于如何继续或是否可行,有什么想法吗?似乎$and操作符目前在MongoDB中不可用。 $and运算符在1.9.1(不稳定版本)中 这是功能请求的票证: 目前(1.8.x)唯一的解决方案似乎使用了多个“$或”语句,如:
even_value_1 AND odd_value_1
OR even_value_1 AND odd_value_2
OR even_value_2 AND odd_value_1
OR event_value_2 AND odd_value_2
编辑:下面是一些帮助未来用户的代码
首先,我们需要一个函数来转换
array_field(event_value_1 OR event_value_2) AND (odd_value_1 OR odd_value_2)
到
下面是所需的函数
/**
* $pArray for our example should be like array(array(event_value_1,
* event_value_2),array(odd_value_1, odd_value_2))
*/
function transform_and_group_of_or_to_or_group_of_and ($pArray)
{
//Make sure we have sequential numerical indexes
sort($pArray);
$maxIndices = array();
foreach ($pArray as $key=>&$values){
//Make sure we have sequential numerical indexes
sort($values);
$maxIndices[$key] = count($values)-1;
$arIndices[$key] = 0;
}
$groupCount = count($pArray);
$groupOfAnd = array();
do {
$newGroup = array();
for ($i=0; $i<$groupCount; $i++){
$indice = $arIndices[$i];
$sousTab = $pArray[$i];
$newGroup[] = $sousTab[$indice];
}
$groupOfAnd[] = $newGroup;
} while(increment_numbers($arIndices, $maxIndices));
return $groupOfAnd;
}
function increment_numbers(& $arIndices, $maxIndices){
//Raise the last indice
$arIndices[count($arIndices)-1]++;
$check = true;
for ($i=count($arIndices)-1; (($i>=0) && ($check === true)); $i--){
if ($arIndices[$i] > $maxIndices[$i]){
if ($i > 0){
$arIndices[$i-1]++;//increment the upper indice element
} else {
return 0;
}
$arIndices[$i] = 0;
$check=true;
}else{
$check = false;
}
}
return true;
}
希望这有帮助
/**
* $pArray for our example should be like array(array(event_value_1,
* event_value_2),array(odd_value_1, odd_value_2))
*/
function transform_and_group_of_or_to_or_group_of_and ($pArray)
{
//Make sure we have sequential numerical indexes
sort($pArray);
$maxIndices = array();
foreach ($pArray as $key=>&$values){
//Make sure we have sequential numerical indexes
sort($values);
$maxIndices[$key] = count($values)-1;
$arIndices[$key] = 0;
}
$groupCount = count($pArray);
$groupOfAnd = array();
do {
$newGroup = array();
for ($i=0; $i<$groupCount; $i++){
$indice = $arIndices[$i];
$sousTab = $pArray[$i];
$newGroup[] = $sousTab[$indice];
}
$groupOfAnd[] = $newGroup;
} while(increment_numbers($arIndices, $maxIndices));
return $groupOfAnd;
}
function increment_numbers(& $arIndices, $maxIndices){
//Raise the last indice
$arIndices[count($arIndices)-1]++;
$check = true;
for ($i=count($arIndices)-1; (($i>=0) && ($check === true)); $i--){
if ($arIndices[$i] > $maxIndices[$i]){
if ($i > 0){
$arIndices[$i-1]++;//increment the upper indice element
} else {
return 0;
}
$arIndices[$i] = 0;
$check=true;
}else{
$check = false;
}
}
return true;
}
if (count($arGroupedCritere)) {
$arGroupedCritere = transform_and_group_of_or_to_or_group_of_and($arGroupedCritere);
foreach($arGroupedCritere as $arCritere) {
$queryBuilder->addOr($queryBuilder->expr()->field('_criteres')->all($arCritere));
}
}