Var*_*roi 10 mongodb node.js aggregation-framework
MongoDb的$ project聚合运算符是否可以将文档重组为数组?
这是我到目前为止所做的:
var pipeline = [];
var project = {
$project : {
x: "$_id",
y: "$y" ,
_id : 0
}
};
pipeline.push(project);
model.aggregate( pipeline, callback);
Run Code Online (Sandbox Code Playgroud)
这给了我输出的形式:
[
{
x: '...',
y: '...'
}
....
]
Run Code Online (Sandbox Code Playgroud)
我想拥有:
[
['..','..']
....
]
Run Code Online (Sandbox Code Playgroud)
我可以通过迭代来轻松地重构输出,但是真的很想知道聚合本身是否可以返回数组而不是对象.
您可以尝试使用 $push 运算符。
例如,如果您有如下文件:
{ _id: <something>, y: 5 }
Run Code Online (Sandbox Code Playgroud)
在 mongo shell 中,如果您输入
db.model.aggregate( [ { $group: { _id: null, newArrayField: { $push: { x: "$_id", y: "$y" } } } } ] )
Run Code Online (Sandbox Code Playgroud)
你会得到:
{
"result" : [
{
"_id" : null,
"newArrayField" : [
{
"x" : ObjectId("5265dd479eb4b1d4289cf222"),
"y" : 5
}
]
}
],
"ok" : 1
}
Run Code Online (Sandbox Code Playgroud)
有关 $push 运算符的更多信息,请参阅http://docs.mongodb.org/manual/reference/operator/aggregation/push/
| 归档时间: |
|
| 查看次数: |
4656 次 |
| 最近记录: |