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

按 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 对你的数组和对象所做的

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

注意,出于实际原因,我修改/删除了一些变量名(_id、oid),您必须解析哪些变量名,如果复制粘贴,此代码很可能无法立即运行。



查看完整回答
反对 回复 2023-02-17
  • 1 回答
  • 0 关注
  • 51 浏览
慕课专栏
更多

添加回答

举报

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