有没有办法从MongoDB中的查询返回特定的嵌套字段? [英] Is there a way to return specific nested fields from a query in MongoDB?

查看:1176
本文介绍了有没有办法从MongoDB中的查询返回特定的嵌套字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取每个运动类别的付款金额? 不仅限于约翰",而且在所有催缴文件中也是如此.

Is there a way to get the amount of money paid per Sport's category? It can't be restricted only to "John," but in all collection documents.

针对所提供数据的预期查询返回:

{
  {"Fight":   [100,95] },
  {"Running": [50]     }
}

数据示例:

{"_id":"5z621578b0ce483b9866fb1f",
 "Name":"John",
 "Sports":[
           {"Category":"Fight",
            "Billing":[
                       {"Month":"Jan",
                        "Status":"Paid",
                        "Price":100},
                      {"Month":"Feb",
                       "Status":"Not Paid",
                       "Price":125}, 
                      {"Month":"Mar",
                       "Status":"Paid",
                       "Price":95}      
                      ]
           },

          {"Category":"Running",
           "Billing":[
                      {"Month":"Jan",
                       "Status":"Not Paid",
                       "Price":200}, 
                      {"Month":"Feb",
                       "Status":"Paid",
                       "Price":50}  
                     ]
          }
      ]
}

换句话说:我需要比较每个嵌套对象中的计费状态,并检查其是否为付费" ,然后如果为true,则分别添加结算对象价格添加到相应运动的类别数组中.

In other words: I need to compare the billing Status inside every nested object and check if it's"Paid", then if true, add the respective billing object Price to the respective Sport's Category array.

对于集合中的所有文档,具有多个运动类别和多个记帐月份.但总是相同的嵌套结构.

For all documents in the collection, with multiple sport's categories and multiple billing's month. But always same nested structure.

提前谢谢!

推荐答案

正如willis在其评论中所说,您将希望使用聚集:

As willis said in his comment, you will want to use aggreagation: https://docs.mongodb.com/manual/aggregation/

以下汇总将为您提供所需的数据(用帐单的实际名称替换帐单):

The following aggregation will give you the data that you are looking for (replace billings with the actual name of your collection):

db.billings.aggregate([
    { $unwind: '$Sports'},
    { $unwind: '$Sports.Billing'},
    { $match: { 'Sports.Billing.Status': 'Paid' } },
    {
        $group: {
            _id: '$Sports.Category',
            Category: {$first: '$Sports.Category'},
            Prices: { $push: '$Sports.Billing.Price' }
        }
    },
    { $project: {_id: 0} }
]);

此聚合的结果如下所示:

The result of this aggregation will look something like this:

[
    {
        "Category" : "Running",
        "Prices" : [ 
            50.0
        ]
    },
    {
        "Category" : "Fight",
        "Prices" : [ 
            100.0, 
            95.0
        ]
    }
]


您在问题中要求的确切格式有点不正确;我认为,最好将其保持在产出之上的汇总形式.但是,如果您希望以问题形式使用它,则聚合会稍微复杂一些:


The exact format that you requested in your question is a bit abnormal; in my opinion, I think it would be better to keep it in the form the aggregation above outputs. But if you want it in a form like the one in your question, the aggregation is a bit more complex:

db.billings.aggregate([
    { $unwind: '$Sports'},
    { $unwind: '$Sports.Billing'},
    { $match: { 'Sports.Billing.Status': 'Paid' } },
    {
        $group: {
            _id: '$Sports.Category',
            Prices: { $push: '$Sports.Billing.Price' }
        }
    },
    {
        $group: {
            _id: 0,
            Sports: { $push: { Category: '$_id', Prices: '$Prices' } }
        }
    },
    {
        $project: {
            Sports: {
                $arrayToObject: {
                    '$map': {
                        'input': '$Sports',
                        'as': 'el',
                        'in': {
                            'k': '$$el.Category',
                            'v': '$$el.Prices'
                        }
                    }
                }
            }
        }
    },
    { $replaceRoot: { newRoot: '$Sports'} }
]);

这篇关于有没有办法从MongoDB中的查询返回特定的嵌套字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆