比如有下面格式的文档
```json
{
_id: ...,
"key": [
{
"subkey1": ...,
"subkey2": ...,
"subkey3": ...
},
{
"subkey1": ...,
"subkey2": ...,
"subkey3": ...
},
...
]
}
```
有可能只使用mongodb直接返回如下格式文档查询结果吗:
```json
[
{
"subkey1": ...,
"subkey2": ...,
"subkey3": ...
},
{
"subkey1": ...,
"subkey2": ...,
"subkey3": ...
},
...
]
```
如果还有node.js的mongodb库代码示例的话,就更好啦^_^
实现你描述的功能可能需要两个 aggregation 操作符:$replaceRoot
, $unwind
:
首先使用 $unwind
把你的 Collection 变成:
[
{"_id":... , key:{"subkey1":xxx, "subkey2":xxxx, "subkey3":xxxx}},
{"_id":... , key:{"subkey1":xxx, "subkey2":xxxx, "subkey3":xxxx}},
{"_id":... , key:{"subkey1":xxx, "subkey2":xxxx, "subkey3":xxxx}}
]
然后使用 $replaceRoot
将 key 节点变成根节点:
[
{
"subkey1": ...,
"subkey2": ...,
"subkey3": ...
},
{
"subkey1": ...,
"subkey2": ...,
"subkey3": ...
},
...
]
用来把 key
数组作为