I need read 2 collections data from MongoDB in Python, is there any way to join data in python?
How to lookup data from 2 collections in MongoDB using python
5.4k views Asked by Dileep Devang At
2
There are 2 answers
0

Have a look here
from bson.objectid import ObjectId
#the custom_id for reference
custom_id = ObjectId()
#creating user with the role admin
db.users.insert_one({"name": "Boston", "role_id": custom_id})
#Creating role with the custom id
db.roles.insert_one({"_id": custom_id, "name": "Admin")}
#lookup usage
db.users.aggregate([
{
"$lookup":
{
"from": "roles",
"localField": "role_id",
"foreignField": "_id",
"as": "roles"
}
}
])
Let's say that we have two collections(tables):
Those tables have the same field 'id_transaction' , and we want to join those tables on this field:
To print results:
For more references: MongoDB Docs and PyMongo Docs