I have two collections and I am using one of collections _id
field as foreign key in other collection. But _id
is Object
and in in other collection it is string
so $lookup
is not able to match.
Collection 1: User collection
{ "_id" : ObjectId("6471bf9db77f0b33a9b0bd38"), "name" : "Alok", "city" : "someCity" }
Collection 2: Order collection
{ "_id" : ObjectId("6471bfd4b77f0b33a9b0bd39"), "uid" : "6471bf9db77f0b33a9b0bd38", "product" : "product1" }
I am trying to use $lookup
db.order.aggregate([{$lookup: {from: 'users', localField:'uid', foreignField:'_id', as:'join'}}])
This $lookup
does not work because _id
is Object and uid
is string.
This is common requirement so there must be some automated way as I dont want to use $lookup with long pipeline.
What can quickest fix to make this $lookup
work without using long pipeline
?