想象我有一个包含文件如下:a MonogDB集合:的MongoDB和PHP - 返回嵌套数组的计数
{name: 'Some Name', components: {ARRAY OF ITEMS}}
我怎样才能返回名称和成分项的个数? 我必须使用地图/缩小吗?
我正在使用PHP的Mongo扩展。
编辑:在PHP的当前代码段(工作),但我只想组件的数量
$fields = array(
'name', 'components'
);
$cursor = $this->collection->find(array(), $fields);
$cursor->sort(array('created_ts' => -1));
if (empty($cursor) == true) {
return array();
} else {
return iterator_to_array($cursor);
}
感谢, 吉姆
请提供样本(查询的预期结果)。 –
阅读问题的第二部分;删除了我的答案。 – Petrogad
示例结果将如{name:'项目名称',组件:3} – JimBlizz