mongodb 组聚合字段“From”必须定义为对象内的表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40792728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
the group aggregate field 'From' must be defined as an expression inside an object
提问by Alex Belke
I have a collection messages in database enron.
我在数据库 enron 中有一个收集消息。
To show the collection structure I use this command.
为了显示集合结构,我使用了这个命令。
db.messages.findOne()
db.messages.findOne()
{
"_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" : "[email protected]",
"Message-ID" : "<32788362.1075840323896.JavaMail.evans@thyme>",
"Mime-Version" : "1.0",
"Subject" : "84029698 Marriott Reservation Confirmation Number",
"To" : [
"[email protected]"
],
"X-FileName" : "eric bass 6-25-02.PST",
"X-Folder" : "\ExMerge - Bass, Eric\Personal",
"X-From" : "[email protected]",
"X-Origin" : "BASS-E",
"X-To" : "[email protected]",
"X-bcc" : "",
"X-cc" : ""
},
"mailbox" : "bass-e",
"subFolder" : "personal"
}
I need to get a result of pairs 'from' and 'to'. To do that I use aggregate command. First of all I use {"$unwind": "$headers.To"}
to ungroup all email from array headers.to. Than I use $group section to group my result.
我需要得到“from”和“to”对的结果。为此,我使用了聚合命令。首先,我使用{"$unwind": "$headers.To"}
从数组 headers.to 中取消所有电子邮件的分组。比我使用 $group 部分来分组我的结果。
I use this query:
我使用这个查询:
db.messages.aggregate([{"$unwind": "$headers.To"},
{"$group": { "_id":null, 'From': "$headers.From", 'To': "$headers.To","count":{$sum:1}}},
{"$sort": {"count": -1}},
{"$limit": 10},
])
Error message:
错误信息:
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
What Did I Do Wrong?
我做错了什么?
回答by Ori Dar
What you want to achieve - assuming you want to group by from & to - can be done with the following group
:
您想要实现的目标 - 假设您想按 from & to 分组 - 可以通过以下方式完成group
:
{"$group": { "_id": {'From': "$headers.From", 'To': "$headers.To"}, "count": {$sum:1}}}
{"$group": { "_id": {'From': "$headers.From", 'To': "$headers.To"}, "count": {$sum:1}}}
From the reference documentation:
从参考文档:
$group
Groups documents by some specified expression and outputs to the next stage a document for each distinct grouping. The output documents contain an _id field which contains the distinct group by key. The output documents can also contain computed fields that hold the values of some accumulator expression grouped by the $group‘s _id field
$组
按某些指定的表达式对文档进行分组,并将每个不同分组的文档输出到下一阶段。输出文档包含一个 _id 字段,其中包含不同的 group by key。输出文档还可以包含计算字段,这些字段保存按 $group 的 _id 字段分组的某些累加器表达式的值
The accumulator expressions are expressions such as min
, max
(per group), sum
(as you've used), arrays expression (specified by push
and addToSet
) and others.
累加器表达式是诸如min
, max
(每组)、sum
(如您所用)、数组表达式(由push
和指定addToSet
)等表达式。
Specifically, the error message notes that you must use one of the above mentioned expressions as a value for the Form
field.
具体来说,错误消息指出您必须使用上述表达式之一作为Form
字段的值。
If you don't like the From
and To
"squeezed" inside _id
, you can later expend it, by adding a $project
stage.
如果你不喜欢里面的From
和To
“挤压” _id
,你可以稍后通过添加一个$project
阶段来消耗它。