Join in mongodb using python

How can I perform join in Mongodb using Python

join in mongo db are done via lookup operation in aggregation, so if you would like to do the join of collection1 with collection2 refer tp https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

Sample code

# Requires the PyMongo package.
# https://api.mongodb.com/python/current

client = MongoClient('mongodb://localhost:27017/?readPreference=primaryPreferred&appname=MongoDB%20Compass&ssl=false')
result = client['db']['collection1'].aggregate([
    {
        '$lookup': {
            'from': 'collection2', 
            'localField': '$collection1field_commom', 
            'foreignField': '$collection2field_commom', 
            'as': 'joinedResult'
        }
    }
])

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.