mongodb的$ project是否可能返回数组?

编程入门 行业动态 更新时间:2024-10-25 21:21:50
本文介绍了mongodb的$ project是否可能返回数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

MongoDb的$ project聚合运算符是否可以将文档重组为数组?

Is it possible for MongoDb's $project aggregation operator to restructure the document to an array ?

这是我到目前为止所做的:

Here is what I did so far:

var pipeline = []; var project = { $project : { x: "$_id", y: "$y" , _id : 0 } }; pipeline.push(project); model.aggregate( pipeline, callback);

这给了我以下形式的输出:

This gives me output of form:

[ { x: '...', y: '...' } .... ]

我想拥有:

[ ['..','..'] .... ]

我可以通过迭代轻松地重组输出,但是真的很想知道聚合本身是否可以返回数组而不是对象.

I can easily restructure the output by iterating it, but really curious to know if aggregate itself could return array instead of object.

推荐答案

您可以尝试使用$ push运算符.

You could try with the $push operator.

例如, 如果您有以下文件:

For example, if you had documents like:

{ _id: <something>, y: 5 }

在mongo shell中,如果您输入

In the mongo shell, if you type

db.model.aggregate( [ { $group: { _id: null, newArrayField: { $push: { x: "$_id", y: "$y" } } } } ] )

您会得到:

{ "result" : [ { "_id" : null, "newArrayField" : [ { "x" : ObjectId("5265dd479eb4b1d4289cf222"), "y" : 5 } ] } ], "ok" : 1 }

有关$ push运算符的更多信息,请参见 docs. mongodb/manual/reference/operator/aggregation/push/

For more information on $push operator, see docs.mongodb/manual/reference/operator/aggregation/push/

更多推荐

mongodb的$ project是否可能返回数组?

本文发布于:2023-11-22 15:17:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617941.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   mongodb   project

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!