第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

按多個(gè)字段劃分的MongoDB組值

按多個(gè)字段劃分的MongoDB組值

胡子哥哥 2019-06-26 13:27:41
按多個(gè)字段劃分的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"}諸若此類。我怎樣才能提出一個(gè)請(qǐng)求,它將描述每個(gè)地址的前N位地址和最上面的M類書籍?預(yù)期結(jié)果實(shí)例:地址1\book_1:5書2:10書3:50共計(jì):65______________________地址2\book_1:10書2:10|...手冊(cè)M:10共計(jì):m*10...______________________地址N_1:20書2:20|...手冊(cè)M:20共計(jì):m*20
查看完整描述

3 回答

?
鳳凰求蠱

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊

下面的查詢將提供與所需響應(yīng)中提供的結(jié)果完全相同的結(jié)果:

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"
            }
        }
    }])

答復(fù)如下:

/* 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}


查看完整回答
反對(duì) 回復(fù) 2019-06-26
  • 3 回答
  • 0 關(guān)注
  • 1161 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)