【问题标题】:Mongo Nested array field selectionMongodb嵌套数组字段选择
【发布时间】:2019-12-06 14:37:52
【问题描述】:

下面是mongodb文档是这样的和这些嵌套数组字段选择:

对象:

    {
        "_id": {
            "$oid": "5de775b53ec85e73da2b6d8a"
        },
        "vpg_id": 2,
        "year": 2019,
        "am_data": {
            "822": {
                "am_name": "Unmanaged ",
                "no_of_mnths": 12,
                "total_invoice": 14476.15,
                "total_bv_invoice": 1840,
                "opp_won_onetime_amt": 0,
                "one_time_quota": 0,
                "recurring_quota": 200,
                "opp_won_rec_amt": 0,
                "avg_total_invoice": 1206.3458333333333,
                "avg_total_bv_invoice": 153.33333333333334,
                "avg_opp_won_onetime_amt": 0,
                "avg_one_time_quota": 0,
                "avg_opp_won_rec_amt": 0,
                "avg_recurring_quota": 16.666666666666668
            },
           "2155": {
                "am_name": "Daniel Schiralli",
                "no_of_mnths": 12,
                "total_invoice": 396814.66000000003,
                "total_bv_invoice": 577693.3200000001,
                "opp_won_onetime_amt": 4792.5,
                "one_time_quota": 14400,
                "recurring_quota": 4800,
                "opp_won_rec_amt": 345,
                "avg_total_invoice": 33067.888333333336,
                "avg_total_bv_invoice": 48141.11000000001,
                "avg_opp_won_onetime_amt": 399.375,
                "avg_one_time_quota": 1200,
                "avg_opp_won_rec_amt": 28.75,
                "avg_recurring_quota": 400
            }
        }
    }

我只想从所有 am_data 数组中选择 no_of_mnthsam_name

钥匙 8222155动态的

它会改变,所以我不能直接在查询中给出它。我如何才能获得这些数据。不想

有什么帮助吗?

【问题讨论】:

    标签: mongodb nested find


    【解决方案1】:

    您可以使用$objectToArray 运算符来摆脱动态键。

    db.getCollection('Test').aggregate([
    
        { $project: {"keys": { "$objectToArray": "$$ROOT.am_data" }} },
        { $unwind : "$keys"},
        { $project: {"am_name":"$keys.v.am_name", "no_of_mnths":"$keys.v.no_of_mnths" } }
    ])
    

    结果:

    [{
        "_id" : ObjectId("5de775b53ec85e73da2b6d8a"),
        "am_name" : "Unmanaged ",
        "no_of_mnths" : 12
    },
    {
        "_id" : ObjectId("5de775b53ec85e73da2b6d8a"),
        "am_name" : "Daniel Schiralli",
        "no_of_mnths" : 12
    }]
    

    【讨论】:

      【解决方案2】:

      密钥不应该改变。根据您的描述,您需要调整架构, 而不是am_data Object,它应该是一个数组。

      {
           "_id": {
               "$oid": "5de775b53ec85e73da2b6d8a"
              },
           "vpg_id": 2,
           "year": 2019,
           "am_data": [
              {
                  "id": "822",
                   "am_name": "Unmanaged ",
                   "no_of_mnths": 12,
                   "total_invoice": 14476.15,
                   "total_bv_invoice": 1840,
                   "opp_won_onetime_amt": 0,
                   "one_time_quota": 0,
                   "recurring_quota": 200,
                   "opp_won_rec_amt": 0,
                   "avg_total_invoice": 1206.3458333333333,
                   "avg_total_bv_invoice": 153.33333333333334,
                   "avg_opp_won_onetime_amt": 0,
                   "avg_one_time_quota": 0,
                   "avg_opp_won_rec_amt": 0,
                   "avg_recurring_quota": 16.666666666666668
              },
              {
                  "id": "2155",
                   "am_name": "Daniel Schiralli",
                   "no_of_mnths": 12,
                   "total_invoice": 396814.66000000003,
                   "total_bv_invoice": 577693.3200000001,
                   "opp_won_onetime_amt": 4792.5,
                   "one_time_quota": 14400,
                   "recurring_quota": 4800,
                   "opp_won_rec_amt": 345,
                   "avg_total_invoice": 33067.888333333336,
                   "avg_total_bv_invoice": 48141.11000000001,
                   "avg_opp_won_onetime_amt": 399.375,
                   "avg_one_time_quota": 1200,
                   "avg_opp_won_rec_amt": 28.75,
                   "avg_recurring_quota": 400
              }
           ]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-25
        • 2012-03-25
        • 1970-01-01
        • 2013-10-07
        相关资源
        最近更新 更多