Mongodb中的汇总查询返回特定字段 [英] Aggregate Query in Mongodb returns specific field

查看:882
本文介绍了Mongodb中的汇总查询返回特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档样本:

{
    "_id" : ObjectId("53329dfgg43771e49538b4567"),
    "u" : {
        "_id" : ObjectId("532a435gs4c771edb168c1bd7"),
        "n" : "Salman khan",
        "e" : "salman@gmail.com"
    },
    "ps" : 0,
    "os" : 1,
    "rs" : 0,
    "cd" : 1395685800,
    "ud" : 0
}

查询:

db.collectiontmp.aggregate([
            {$match: {os:1}},
            {$project : { name:{$toUpper:"$u.e"} , _id:0 } },
            {$group: { _id: "$u._id",total: {$sum:1} }},
            {$sort: {total: -1}}, { $limit: 10 }
             ]);

我需要上述查询中的以下内容:

I need following things from the above query:

  1. u._id
  2. 分组
  3. 返回记录总数和记录中的电子邮件,如下所示:

  1. Group by u._id
  2. Returns total number of records and email from the record, as shown below:

{ 结果": [ { 电子邮件": "", 全部的": "" }, { 电子邮件": "", 全部的": "" } ], 好的": 1个 }

{ "result": [ { "email": "", "total": "" }, { "email": "", "total": "" } ], "ok": 1 }

推荐答案

您在这里做错的第一件事是不了解 $project 用于工作.管道阶段,例如 $project $group 仅输出被明确"标识的字段.因此,只有您说要输出的字段可用于以下管道阶段.

The first thing you are doing wrong here is not understanding how $project is intended to work. Pipeline stages such as $project and $group will only output the fields that are "explicitly" identified. So only the fields you say to output will be available to the following pipeline stages.

特别是在这里,您仅投影"文档中"u"字段的一部分,因此删除了其他可用数据.现在,这里唯一存在的字段是名称",即您投影的"字段.

Specifically here you "project" only part of the "u" field in your document and you therefore removed the other data from being available. The only present field here now is "name", which is the one you "projected".

也许您真的打算这样做:

Perhaps it was really your intention to do something like this:

db.collectiontmp.aggregate([
    { "$group": {
        "_id": {
           "_id": "$u._id",
           "email": { "$toUpper": "$u.e" }
        },
        "total": { "$sum": 1 },
    }},
    { "$project": {
        "_id": 0,
        "email": "$_id.email",
        "total": 1
    }},
    { "$sort": { "total": -1 } },
    { "$limit": 10 }
])

甚至:

db.collectiontmp.aggregate([
    { "$group": {
        "_id": "$u._id",
        "email": { "$first": { "$toUpper": "$u.e" } }
        "total": { "$sum": 1 },
    }},
    { "$project": {
        "_id": 0,
        "email": 1,
        "total": 1
    }},
    { "$sort": { "total": -1 } },
    { "$limit": 10 }
])

这为您提供了所需的输出.

That gives you the sort of output you are looking for.

请记住,由于这是管道",因此只有下一个阶段的输出"才可用于下一个"阶段.该文档没有全局"概念,因为它不是诸如SQL中的声明性语句,而是管道".

Remember that as this is a "pipeline", then only the "output" from a prior stage is available to the "next" stage. There is no "global" concept of the document as this is not a declarative statement such as in SQL, but a "pipeline".

所以请考虑Unix管道"|"命令,否则请查收.然后您的想法就会落伍.

So think Unix pipe "|" command, or otherwise look that up. Then your thinking will fall into place.

这篇关于Mongodb中的汇总查询返回特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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