在mongodb聚合中包含字段 [英] Include fields in mongodb aggregate

查看:149
本文介绍了在mongodb聚合中包含字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下收藏:

{"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"}
{"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"}
{"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"}
{"orderID" : "89765", "branch" : "CO", "customerID" : "54157526", "customerEmail" : ""}
{"orderID" : "89765", "branch" : "CO", "customerID" : "54157526", "customerEmail" : ""}
{"orderID" : "21546", "branch" : "CO", "customerID" : "20103585", "customerEmail" : "xxx@yyy.com"}
{"orderID" : "21546", "branch" : "CO", "customerID" : "20103585", "customerEmail" : "xxx@yyy.com"}
{"orderID" : "21546", "branch" : "KA", "customerID" : "89374792", "customerEmail" : "aaa@ccc.com"}
{"orderID" : "21794", "branch" : "NY", "customerID" : "78125522", "customerEmail" : ""}

我需要在customerEmail不为null的某个分支中获取所有唯一的客户ID.我对分支"的期望:"CO"

I need to get all unique customerIDs in a certain branch whose customerEmail is not null. What I expect for "branch":"CO"

{"customerID" : "11396783", "customerEmail" : "foo@bar.com"}
{"customerID" : "20103585", "customerEmail" : "xxx@yyy.com"}

到目前为止,我已经尝试过:

So far I've tried:

db.collection.aggregate([
    { $match: { branch: "CO" } },   
    { $group:
        {
            _id: { customer:"$customerID"}
        }
    },
    {
        $group: {_id:"$_id.customer"}
    },
    {
        $addFields: {  email: "$customerEmail"} 
    }
]);

但它不带电子邮件字段.

but it doesn't bring the email field.

推荐答案

它不包含该字段,因为您没有要求该字段返回.您在这里缺少的是使用 $first 或类似的累加器" ,以便在 $group .

It does not include the field because you did not ask for the field to return. The thing you are missing here is using $first or a similar "accumulator" in order to return the element during the $group.

如果您不想使用空的电子邮件地址,也可以将其排除在

Also if you don't want the empty email address then exclude it within the $match pipeline stage, since that's the most efficient thing to do.

db.collection.aggregate([
    { $match: { branch: "CO", "customerEmail": { "$ne": "" } } },
    { $group:
        {
            _id: { customer:"$customerID"},
            email: { "$first": "$customerEmail" }
        }
    }
]);

管道"仅从类似 或您实际要求的 $project 它来.就像"Unix管道" |运算符一样,下一个阶段"可用的唯一内容就是输出的内容.

A "pipeline" only returns "output" from stages like $group or $project that you actually ask it to. Just like the "Unix pipe" | operator, the only things available to the "next stage" are what you output.

这应该从以下内容中显而易见:

This should be evident simply from:

db.collection.aggregate([
    { $match: { branch: "CO" } },   
    { $group:
        {
            _id: { customer:"$customerID"}
        }
    }
]);

甚至:

db.collection.aggregate([
    { $match: { branch: "CO" } },   
    { $project:
        {
            _id: { customer:"$customerID"}
        }
    }
]);

当然,其中只返回_id值,因为这是您所要求的.

Which returns of course only the _id value since that is all you asked for.

您只能在任何管道阶段中访问上一阶段输出"的数据.在 $group 中,这意味着仅_id分组密钥,以及使用有效的明确" 指定的任何内容="nofollow noreferrer">累加器" ,以获取您希望返回的任何其他属性.任何累加器(在此对字符串"有效)都可以,但是_id 必须之外的任何内容都必须使用.

You only have access in any pipeline stage to the data which was "output by the previous stage". Within a $group that means only the _id for the grouping key, and whatever was specified "explicitly" using a valid "accumulator" for any other properties you wish to return. Any accumulator ( which is valid for a "string" here ) will do, but anything outside of the _id must use an "accumulator".

我建议花点时间看一下所有聚合运算符以及他们的实际工作.每个运算符都有示例用法

I suggest taking the time to look at all the aggregation operators and what they actually do. There is example usage with each operator

这篇关于在mongodb聚合中包含字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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