为了账号安全,请及时绑定邮箱和手机立即绑定

按多个字段划分的MongoDB组值

按多个字段划分的MongoDB组值

胡子哥哥 2019-06-26 13:27:41
按多个字段划分的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
查看完整描述

3 回答

?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

下面的查询将提供与所需响应中提供的结果完全相同的结果:

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}


查看完整回答
反对 回复 2019-06-26
  • 3 回答
  • 0 关注
  • 1114 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信