首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MongoDB 在查询时要求得到总和,但是数据中存在数组节点的计算,该怎么做?

MongoDB 在查询时要求得到总和,但是数据中存在数组节点的计算,该怎么做?

提问于 2019-05-24 11:17:18
回答 0关注 0查看 505

查询MongoDB的时候想直接把总和带出来而不是在代码中去计算

我这边由于才开始使用MongoDB不了解如果在查询计算中处理数组

这里需要把 service节点中的 每个对象中的price节点中的value值相加得到这个订单的总金额,求大佬们给小弟指导一下

数据示例:

代码语言:js
复制
{
     "project": {
         "name":xxx,
         "id":112233
     }, 
      "comment": null, 
      "evaluation": 5, 
      "created": 1559380560020, 
      "serviceDate": 1559380655784, 
      "finishDate": 1559384199416, 
      "serialNumber": 20190429322250, 
       "service": [
                {
                    "_id": "5ab9c44fc163db1a123c68da", 
                    "name": "代办代买", 
                    "price": {
                        "unit": "元", 
                        "categoryUnit": "次", 
                        "category": "SINGLE", 
                        "unitPrice": 3, 
                        "value": 3, 
                        "count": 1
                    }
                }, 
                {
                    "_id": "5b20d380d9fe6121d3517587", 
                    "name": "打扫房屋卫生", 
                    "price": {
                        "unit": "元", 
                        "categoryUnit": "次", 
                        "category": "MULTI", 
                        "unitPrice": 1, 
                        "value": 42, 
                        "count": 42
                    }
                }, 
                {
                    "_id": "5ab9c44f7b571d1a2e766513", 
                    "name": "上门理发", 
                    "price": {
                        "unit": "元", 
                        "categoryUnit": "次", 
                        "category": "SINGLE", 
                        "unitPrice": 10, 
                        "value": 10, 
                        "count": 1
                    }
                }, 
                {
                    "_id": "5ab9c45068665e1a11c31a03", 
                    "name": "剪指甲", 
                    "price": {
                        "unit": "元", 
                        "categoryUnit": "次", 
                        "category": "SINGLE", 
                        "unitPrice": 3, 
                        "value": 3, 
                        "count": 1
                    }
                }, 
                {
                    "_id": "5acdb3492f78963f6d2126ad", 
                    "name": "血压.血糖测量,心率.基本生命体征检查等.并建立健康档案", 
                    "price": {
                        "unit": "元", 
                        "categoryUnit": "次", 
                        "category": "SINGLE", 
                        "unitPrice": 25, 
                        "value": 25, 
                        "count": 1
                    }
                }, 
                {
                    "_id": "5ab9c450c163db1a123c68dc", 
                    "name": "按摩", 
                    "price": {
                        "unit": "元", 
                        "categoryUnit": "次", 
                        "category": "SINGLE", 
                        "unitPrice": 20, 
                        "value": 20, 
                        "count": 1
                    }
                }, 
                {
                    "_id": "5acc41c72f78963f6d2125fc", 
                    "name": "中药泡脚", 
                    "price": {
                        "unit": "元", 
                        "categoryUnit": "次", 
                        "category": "SINGLE", 
                        "unitPrice": 30, 
                        "value": 30, 
                        "count": 1
                    }
                }, 
                {
                    "_id": "5ab9c450515a711a286b20d3", 
                    "name": "精神慰藉", 
                    "price": {
                        "unit": "元", 
                        "categoryUnit": "次", 
                        "category": "SINGLE", 
                        "unitPrice": 5, 
                        "value": 5, 
                        "count": 1
                    }
                }, 
                {
                    "_id": "5acdb349809a383f52a3c1cc", 
                    "name": "灭蚊、灭蟑螂、杀虫", 
                    "price": {
                        "unit": "元", 
                        "categoryUnit": "次", 
                        "category": "SINGLE", 
                        "unitPrice": 10, 
                        "value": 10, 
                        "count": 1
                    }
                }, 
                {
                    "_id": "5acdb34959bda23f73129961", 
                    "name": "健康和法律知识讲座", 
                    "price": {
                        "unit": "元", 
                        "categoryUnit": "次", 
                        "category": "SINGLE", 
                        "unitPrice": 5, 
                        "value": 5, 
                        "count": 1
                    }
                }
            ]
}

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档