I want to use $lookup to join two collections in my database. I need to take primary keys("_id") from one collection and add them to another collection as “id2” in order to use lookup. How can I perform this operation?
I want to copy the “_id” for this collection to another collection and save it as “_id2”
in another collection. And them perform $lookup and get both the collections together with my foreignField being “_id2”.
Untested:
collection_one = ...
collection_two = ...
document_one = { ... }
collection_one.insertOne( document_one )
document_two = { "_id2" : document_one._id }
collection_two.insertOne( document_two )
lookup = { "$lookup" : {
"from" : "collection_two" ,
"localField" : "_id" ,
"foreignField" : "_id2" ,
"as" : "two"
} }
collection_one.aggregate( lookup )