按多个字段划分的MongoDB组值

按多个字段划分的MongoDB组值

例如,我有以下文件:

{
  "addr": "address1",
  "book": "book1"},{
  "addr": "address2",
  "book": "book1"},{
  "addr": "address1",
  "book": "book5"},{
  "addr": "address3",
  "book": "book9"},{
  "addr": "address2",
  "book": "book5"},{
  "addr": "address2",
  "book": "book1"},{
  "addr": "address1",
  "book": "book1"},{
  "addr": "address15",
  "book": "book1"},{
  "addr": "address9",
  "book": "book99"},{
  "addr": "address90",
  "book": "book33"},{
  "addr": "address4",
  "book": "book3"},{
  "addr": "address5",
  "book": "book1"},{
  "addr": "address77",
  "book": "book11"},{
  "addr": "address1",
  "book": "book1"}

诸若此类。


我怎样才能提出一个请求,它将描述每个地址的前N位地址和最上面的M类书籍?

预期结果实例:

地址1\book_1:5
书2:10
书3:50
共计:65
______________________
地址2\book_1:10
书2:10
|...
手册M:10
共计:m*10
...
______________________
地址N_1:20
书2:20
|...
手册M:20
共计:m*20


胡子哥哥
浏览 1115回答 3
3回答

凤凰求蛊

下面的查询将提供与所需响应中提供的结果完全相同的结果:db.books.aggregate([     {         $group: {             _id: { addresses: "$addr", books: "$book" },             num: { $sum :1 }         }     },     {         $group: {             _id: "$_id.addresses",             bookCounts: { $push: { bookName: "$_id.books",count: "$num" } }         }     },     {         $project: {             _id: 1,             bookCounts:1,             "totalBookAtAddress": {                 "$sum": "$bookCounts.count"             }         }     }])答复如下:/* 1 */{     "_id" : "address4",     "bookCounts" : [         {             "bookName" : "book3",             "count" : 1         }     ],     "totalBookAtAddress" : 1},/* 2 */{     "_id" : "address90",     "bookCounts" : [         {             "bookName" : "book33",             "count" : 1         }     ],     "totalBookAtAddress" : 1},/* 3 */{     "_id" : "address15",     "bookCounts" : [         {             "bookName" : "book1",             "count" : 1         }     ],     "totalBookAtAddress" : 1},/* 4 */{     "_id" : "address3",     "bookCounts" : [         {             "bookName" : "book9",             "count" : 1         }     ],     "totalBookAtAddress" : 1},/* 5 */{     "_id" : "address5",     "bookCounts" : [         {             "bookName" : "book1",             "count" : 1         }     ],     "totalBookAtAddress" : 1},/* 6 */{     "_id" : "address1",     "bookCounts" : [         {             "bookName" : "book1",             "count" : 3         },         {             "bookName" : "book5",             "count" : 1         }     ],     "totalBookAtAddress" : 4},/* 7 */{     "_id" : "address2",     "bookCounts" : [         {             "bookName" : "book1",             "count" : 2         },         {             "bookName" : "book5",             "count" : 1         }     ],     "totalBookAtAddress" : 3},/* 8 */{     "_id" : "address77",     "bookCounts" : [         {             "bookName" : "book11",             "count" : 1         }     ],     "totalBookAtAddress" : 1},/* 9 */{     "_id" : "address9",     "bookCounts" : [         {             "bookName" : "book99",             "count" : 1         }     ],     "totalBookAtAddress" : 1}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MongoDB