问题描述
我的应用程序中有一个字段,如下所示。
{
"Ct": "HH",Val:{
"Count":"A","Branch":"A"
}
}
当我尝试使用CouchDB中的以下命令检索此记录时,我无法检索记录。
{
"selector" : {
"Val":{
"$elemmatch":{
"Count":"A"
}
}
}
解决方法
从CouchDB文档中,public function json(Export $export)
{
$buffer = $export->getCsvData_();
$file = response()->streamDownload(
function () use ($buffer) {
$file = fopen('php://output','w');
fputs($file,$buffer);
fclose($file);
},"$export->filename.csv",[
"Content-type" => "text/csv","Content-Disposition" => "attachment; filename=$export->filename.csv","Pragma" => "no-cache","Cache-Control" => "must-revalidate,post-check=0,pre-check=0","Expires" => "0"
]
);
//$file is the csv file URL
$CSV_Data_Array = [];
$file_handle = fopen($file,'r');
while (!feof($file_handle) ) {
$CSV_Data_Array[] = fgetcsv($file_handle);
}
fclose($file_handle);
//$csv = file_get_contents($file);
print_r($CSV_Data_Array);
//$array = array_map("str_getcsv",explode("\n",$csv));
$json = json_encode($array);
print_r($json);
}
[1]
匹配并返回包含带有at的数组字段的所有文档 至少一个符合所有指定查询条件的元素。
$elemMatch
不是数组字段,因此Val.Count
不适用。
考虑有关子字段查询的CouchDB文档 [2] :
1.3.6.1.3。子字段
更复杂的选择器使您可以为的字段指定值 嵌套对象或子字段。例如,您可以使用标准 用于指定字段和子字段的JSON结构。
使用标准JSON的字段和子字段选择器的示例 结构:
$elemMatch
缩写等效词使用点表示法组合字段和 子字段名称合并为一个名称。
{ "imdb": { "rating": 8 } }
具体地说,
{
"imdb.rating": 8
}
1 CouchDB: 1.3.6.1.7. Combination Operators
2 CouchDB: 1.3.6.1.3. Subfields