亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按 id 分組并按 mongoose express 節點 js 中的總計數獲取訂單

按 id 分組并按 mongoose express 節點 js 中的總計數獲取訂單

12345678_0001 2023-02-17 17:26:21
我有一個這樣的集合:{"_id": {    "$oid": "5f54b3333367b91bd09f4485"},"products": [    {        "_id": 20,        "name": "Türk Kahvesi",        "price": 8,        "count": 2    },    {        "_id": 22,        "name": "Dibek",        "price": 10,        "count": 2    },    {        "_id": 21,        "name": "Damla Sak?zl? T.K.",        "price": 10,        "count": 1    }],"deskId": "5f53473611f7490d3c860ccd","waiterId": "1","deskName": "ü2", },{"_id": {    "$oid": "5f54af663367b91bd09f4483"},"products": [    {        "_id": 20,        "name": "Türk Kahvesi",        "price": 8,        "count": 1    },    {        "_id": 21,        "name": "Damla Sak?zl? T.K.",        "price": 10,        "count": 1    },    {        "_id": 22,        "name": "Dibek",        "price": 10,        "count": 1    },    {        "_id": 23,        "name": "Menengi?",        "price": 10,        "count": 1    },    {        "_id": 25,        "name": "Double Espresso",        "price": 15,        "count": 6    }],"deskId": "5f53473611f7490d3c860ccd","waiterId": "1","deskName": "ü2", }我的目標是,獲取所有數據順序并按 product._id 分組并顯示 totalPrice(price*count)、products.name、_id 和 totalCount。這實際上是一個產品一個產品地給我們展示一個報告。如果您需要更多信息,請詢問。示例結果:{{    "_id": 20,    "name": "Türk Kahvesi",    "totalCount": 3,    "totalPrice": 24},{    "_id": 22,    "name": "Dibek",    "totalCount": 3,    "totalPrice": 30},{    "_id": 21,    "name": "Damla Sak?zl? T.K.",    "totalCount": 2,    "totalPrice": 20},{    "_id": 23,    "name": "Menengi?",    "totalCount": 1,    "totalPrice": 10},{    "_id": 25,    "name": "Double Espresso",    "totalCount": 6,    "totalPrice": 90}}我們可以在某些東西上使用聚合。請幫我。
查看完整描述

1 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

我對 MongoDB 還是個新手,但我認為這個聚合管道正是您要找的。也就是說,這是您應該使用文檔自己研究的事情,但只要您了解思維過程,您就會學到一些東西,所以一切都很好!


[


        {

          $unwind: {

            path: '$products',

            // Here we are seperating each item in the products array of

            // the user(I presumed your objects were users, or carts maybe)

            // It will now be available to the next stage of the pipeline as

            // a singular object for each item in the array,

            // see the picture below for how this works practically.

          }

        },

        {

          $group: {

            // Now we're going to restructure the object to

            // center around the id field of the products, and

            // at the same time we can add up the total price

            // and count of each item.

            _id: '$products.id', // This is the selector for the grouping process (in our case it's the id)

            item: { $first: '$$ROOT.products' }, // this is me thinking you'll want access to the item in question for each total.

            totalCount: { $sum: "$products.count" }, // adds to totalCount EACH $products.count that we have

            totalPrice: { $sum: { $multiply: ["$products.price", '$products.count'] } }, // self explanatory

          }

        }

]

這就是 unwind 對你的數組和對象所做的

http://img1.sycdn.imooc.com//63ef488b00011a4715380517.jpg

注意,出于實際原因,我修改/刪除了一些變量名(_id、oid),您必須解析哪些變量名,如果復制粘貼,此代碼很可能無法立即運行。



查看完整回答
反對 回復 2023-02-17
  • 1 回答
  • 0 關注
  • 91 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號