2016-11-24 14 views
11

Tôi có một thư bộ sưu tập trong cơ sở dữ liệu enron.trường tổng hợp nhóm 'Từ' phải được định nghĩa là một biểu thức bên trong một đối tượng

Để hiển thị cấu trúc bộ sưu tập, tôi sử dụng lệnh này.

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" : "<[email protected]>", 
     "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" 
} 

tôi cần để có được một kết quả của cặp 'từ' và 'thành'. Để làm điều đó tôi sử dụng lệnh tổng hợp. Trước hết, tôi sử dụng {"$unwind": "$headers.To"} để hủy nhóm tất cả email khỏi mảng headers.to. Hơn tôi sử dụng phần $ nhóm để nhóm kết quả của tôi.

tôi sử dụng truy vấn này:

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

Thông báo lỗi:

assert: command failed: { 
    "ok" : 0, 
    "errmsg" : "the group aggregate field 'From' must be defined as an expression inside an object", 
    "code" : 15951 
} : aggregate failed 
[email protected]/mongo/shell/utils.js:25:13 
[email protected]/mongo/shell/assert.js:13:14 
[email protected]/mongo/shell/assert.js:287:5 
[email protected]/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 : 
[email protected]/mongo/shell/utils.js:25:13 
[email protected]/mongo/shell/assert.js:13:14 
[email protected]/mongo/shell/assert.js:287:5 
[email protected]/mongo/shell/collection.js:1312:5 
@(shell):1:1 

Ý của tôi làm gì sai?

Trả lời

15

gì bạn muốn đạt được - giả sử bạn muốn nhóm phát từ & tới - có thể được thực hiện với sự sau group:

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

Từ các tài liệu tham khảo:

$ group

Nhóm tài liệu theo một số biểu thức và đầu ra được chỉ định cho giai đoạn tiếp theo một tài liệu cho từng nhóm riêng biệt. Các tài liệu đầu ra chứa một trường _id chứa nhóm riêng biệt theo khóa. Các văn bản đầu ra cũng có thể chứa các lĩnh vực tính toán rằng giữ các giá trị của một số biểu hiện ắc nhóm lại theo lĩnh vực _id các $ nhóm

Các biểu thức ắc là những biểu hiện như min, max (mỗi nhóm), sum (như bạn đã sử dụng), biểu thức mảng (được chỉ định bởi pushaddToSet) và các biểu thức khác.

Cụ thể, thông báo lỗi ghi chú rằng bạn phải sử dụng một trong các biểu thức được đề cập ở trên làm giá trị cho trường Form.

Nếu bạn không thích FromTo "vắt" bên trong _id, sau đó bạn có thể sử dụng nó, bằng cách thêm một giai đoạn $project.

+0

Tại sao tôi không thể nhăn nheo như thế này? {"$ group": {"_id": null, 'From': "$ headers.From", 'To': "$ headers.To", "count": {$ sum: 1}}} –

+2

Bởi vì - như đã đề cập - pha nhóm chỉ có thể bao gồm '_id' và các biểu thức được đề cập –

Các vấn đề liên quan