0

Below is a database that i have created by mapping one database to another using dot notation:

for ladders in season_ladders_db2.ladders.find({},{'tier.division.ladder_id':1}):
    db_ladder_id.ladders.insert_one(ladders)

The output:

{'_id': ObjectId('5baa8d09a4918a1cc0ed16e2'),
 'tier': [{'division': [{'ladder_id': 198440},
                        {'ladder_id': 197477},
                        {'ladder_id': 198936},
                        {'ladder_id': 197279},
                        {'ladder_id': 199169},
                        {'ladder_id': 197611},
                        {'ladder_id': 197365},
                        {'ladder_id': 197803},
                        {'ladder_id': 198683},
                        {'ladder_id': 197062}]},
          {'division': [{'ladder_id': 197053},
                        {'ladder_id': 198094},
                        {'ladder_id': 199170},
                        {'ladder_id': 198417},
                        {'ladder_id': 197792},
                        {'ladder_id': 197342},
                        {'ladder_id': 197507}]},
          {'division': [{'ladder_id': 197310},
                        {'ladder_id': 197620},
                        {'ladder_id': 197968},
                        {'ladder_id': 198774},
                        {'ladder_id': 197405},
                        {'ladder_id': 198366},
                        {'ladder_id': 197065},
                        {'ladder_id': 199163},
                        {'ladder_id': 197522},
                        {'ladder_id': 198550},
                        {'ladder_id': 198132},
                        {'ladder_id': 197780},
                        {'ladder_id': 198992}]}]}
{'_id': ObjectId('5baa8d0aa4918a1cc0ed16e3'),
 'tier': [{'division': [{'ladder_id': 197921}]}]}

Using Python and PyMongo how can I build an array of simply the 'ladder_id' values?

The output need is something like

[198440, 197477, 198936, ...] 

is there a way i can pass these values within mongodb? perhaps to another collection? I want to pass these values to an API call.

Thank you!

1 Answer 1

1

In this case, you need to unpack($unwind) values and project($project) ladder_id to the root of the query result.

db.getCollection('test').aggregate(
[
  {$unwind: '$tier'},
])

This query will literally unwind tier, meaning all division will have it's own tier.

{
    "_id" : ObjectId("5baace3569cb14ccb9e5dfc5"),
    "tier" : {
         "division" : [ 
            {
                "ladder_id" : 197921
            }
        ]
    }
}, {}, {}, .....

Then, you need to unwind those divisions so all those ladder_ids will have it's own tier and division.

db.getCollection('test').aggregate(
[
  {$unwind: '$tier'},
  {$unwind: '$tier.division'},
])

Result:

{
    "_id" : ObjectId("5baace2769cb14ccb9e5df34"),
    "tier" : {
        "division" : {
            "ladder_id" : 198440
        }
    }
}, {}, {}, .....

Finally, you can $project those ladder_ids.

db.getCollection('test').aggregate(
[
  {$unwind: '$tier'},
  {$unwind: '$tier.division'},
  {$project: {ladder_id: '$tier.division.ladder_id'}},
])

Result:

{
    "_id" : ObjectId("5baace2769cb14ccb9e5df34"),
    "ladder_id" : 198440
}, {}, {}, .....
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! It took me way longer than it should have to realize that '$unwind' needed to be in quotes, but once I fixed that, this got me there. Thank you!!!

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.