首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >必须将组聚合字段'From‘定义为对象内的表达式。

问必须将组聚合字段'From‘定义为对象内的表达式。
EN

Stack Overflow用户
提问于 2016-11-24 18:27:50
回答 1查看 29.5K关注 0票数 14

我在enron数据库中有一组信息。

为了显示集合结构,我使用以下命令。

db.messages.findOne()

代码语言:javascript
复制
{
    "_id" : ObjectId("4f16fc97d1e2d32371003f02"),
    "body" : "COURTYARD\n\nMESQUITE\n2300 HWY 67\nMESQUITE, TX  75150\ntel: 972-681-3300\nfax: 972-681-3324\n\nHotel Information: http://courtyard.com/DALCM\n\n\nARRIVAL CONFIRMATION:\n Confirmation Number:84029698\nGuests in Room: 2\nNAME: MR ERIC  BASS \nGuest Phone: 7138530977\nNumber of Rooms:1\nArrive: Oct 6 2001\nDepart: Oct 7 2001\nRoom Type: ROOM - QUALITY\nGuarantee Method:\n Credit card guarantee\nCANCELLATION PERMITTED-BEFORE 1800 DAY OF ARRIVAL\n\nRATE INFORMATION:\nRate(s) Quoted in: US DOLLAR\nArrival Date: Oct 6 2001\nRoom Rate: 62.10  per night. Plus tax when applicable\nRate Program: AAA AMERICAN AUTO ASSN\n\nSPECIAL REQUEST:\n NON-SMOKING ROOM, GUARANTEED\n   \n\n\nPLEASE DO NOT REPLY TO THIS EMAIL \nAny Inquiries Please call 1-800-321-2211 or your local\ninternational toll free number.\n \nConfirmation Sent: Mon Jul 30 18:19:39 2001\n\nLegal Disclaimer:\nThis confirmation notice has been transmitted to you by electronic\nmail for your convenience. Marriott's record of this confirmation\nnotice is the official record of this reservation. Subsequent\nalterations to this electronic message after its transmission\nwill be disregarded.\n\nMarriott is pleased to announce that High Speed Internet Access is\nbeing rolled out in all Marriott hotel brands around the world.\nTo learn more or to find out whether your hotel has the service\navailable, please visit Marriott.com.\n\nEarn points toward free vacations, or frequent flyer miles\nfor every stay you make!  Just provide your Marriott Rewards\nmembership number at check in.  Not yet a member?  Join for free at\nhttps://member.marriottrewards.com/Enrollments/enroll.asp?source=MCRE\n\n",
    "filename" : "2.",
    "headers" : {
        "Content-Transfer-Encoding" : "7bit",
        "Content-Type" : "text/plain; charset=us-ascii",
        "Date" : ISODate("2001-07-30T22:19:40Z"),
        "From" : "reservations@marriott.com",
        "Message-ID" : "<32788362.1075840323896.JavaMail.evans@thyme>",
        "Mime-Version" : "1.0",
        "Subject" : "84029698 Marriott  Reservation Confirmation Number",
        "To" : [
            "ebass@enron.com"
        ],
        "X-FileName" : "eric bass 6-25-02.PST",
        "X-Folder" : "\\ExMerge - Bass, Eric\\Personal",
        "X-From" : "Reservations@Marriott.com",
        "X-Origin" : "BASS-E",
        "X-To" : "EBASS@ENRON.COM",
        "X-bcc" : "",
        "X-cc" : ""
    },
    "mailbox" : "bass-e",
    "subFolder" : "personal"
}

我需要得到“从”和“到”的结果。为此,我使用聚合命令。首先,我使用{"$unwind": "$headers.To"}从数组headers.to取消所有电子邮件组。而不是使用$group部分来分组我的结果。

我使用以下查询:

代码语言:javascript
复制
db.messages.aggregate([{"$unwind": "$headers.To"},
    {"$group": { "_id":null, 'From': "$headers.From", 'To': "$headers.To","count":{$sum:1}}},
    {"$sort": {"count": -1}},
    {"$limit": 10},
    ])

错误消息:

代码语言:javascript
复制
assert: command failed: {
    "ok" : 0,
    "errmsg" : "the group aggregate field 'From' must be defined as an expression inside an object",
    "code" : 15951
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:287:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1312:5
@(shell):1:1

2016-11-24T20:07:50.827+0200 E QUERY    [thread1] Error: command failed: {
    "ok" : 0,
    "errmsg" : "the group aggregate field 'From' must be defined as an expression inside an object",
    "code" : 15951
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:287:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1312:5
@(shell):1:1

我做错什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-24 18:46:04

您想要达到的目标--假设您希望通过以下group进行分组--

{"$group": { "_id": {'From': "$headers.From", 'To': "$headers.To"}, "count": {$sum:1}}}

参考文件中:

$group 按指定的表达式对文档进行分组,并将每个不同分组的文档输出到下一阶段。输出文档包含一个_id字段,该字段包含按键划分的不同组。输出文档还可以包含计算字段,这些字段保存按$group的_id字段分组的累加器表达式的值。

累加器表达式是一些表达式,如min、max (每个组)、sum (正如您已经使用过)、数组表达式(由push和addToSet指定)等等。

特别是,错误消息指出,必须使用上述表达式之一作为Form字段的值。

如果您不喜欢From和To在_id中“压缩”,您以后可以通过添加$project阶段来扩展它。

票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40792728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档