如何使用聚合框架排除_id而不包含其他字段 [英] How to exclude _id without including other fields using the aggregation framework

查看:90
本文介绍了如何使用聚合框架排除_id而不包含其他字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取不带_id的聚合管道的结果.我知道,如果您明确提供其他字段作为投影的输出,则这是可能的.但是,我怎么能在查找呼叫中模仿$ projec?

I would like to get the result of an aggregation pipeline without the _id filed. I know this is possible if you provide explicitly other fields that will be the output of the projection. But, ¿how can I mimic the $projec in a find call?

这就是我想要的(未明确包括任何字段):

db.col.find({},{_id:0})

但是在聚合框架中似乎是不可能的:

But in the aggregation framework seems to be impossible:

db.col.aggregate([{'$project': {_id:0}}])

Error: Printing Stack Trace
    at printStackTrace (src/mongo/shell/utils.js:37:15)
    at DBCollection.aggregate (src/mongo/shell/collection.js:927:9)
    at (shell):1:11
2013-10-07T16:36:09.273+0200 aggregate failed: {
    "errmsg" : "exception: $projection requires at least one output field",
    "code" : 16403,
    "ok" : 0
} at src/mongo/shell/collection.js:928

有解决此问题的主意吗?

Any idea to workaround this issue ?

推荐答案

使用聚合时,必须显式

When using aggregation, you must explicitly include/exclude fields. So, you'd need to list all the fields you want. It's not equivalent to find. So, you might:

db.sample.aggregate(
    { $project : {
        _id : 0,
        title : 1             
    }}
);

使用聚合框架还附带一些限制意识到.它是专为聚合(分组,求和等)而设计的,因此投影中不包含很多字段(通常会导致结果超过允许的最大值,即16MB).

Using the aggregation framework also comes with some limits you should be aware of. It's designed for aggregation (grouping, summing, etc.), so having many fields in a projection isn't as typical (and could cause results to exceed the maximum allowed, which is 16MB).

这篇关于如何使用聚合框架排除_id而不包含其他字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆