1

Below is mongodb document is like this and these nested array field selection:

object :

    {
        "_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
            }
        }
    }

I want to select only no_of_mnths and am_name from all am_data arrays.

The keys 822 and 2155 is dynamic.

It will change so i cannot directly give it in query. How i can approach to get this data. Don't want

Any help ?

2 Answers 2

1

You can use $objectToArray operator to get rid of the dynamic keys.

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" } }
])

Result:

[{
    "_id" : ObjectId("5de775b53ec85e73da2b6d8a"),
    "am_name" : "Unmanaged ",
    "no_of_mnths" : 12
},
{
    "_id" : ObjectId("5de775b53ec85e73da2b6d8a"),
    "am_name" : "Daniel Schiralli",
    "no_of_mnths" : 12
}]
Sign up to request clarification or add additional context in comments.

Comments

0

the key should not change. base on your description you need to adjust the schema, instead of am_data Object it should be an array instead.

{
     "_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
        }
     ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.