I am performing joins in mongodb using $lookup, now i am facing a problem here. I have two collections first one to contains users all bookmarks brands and the second one co
You cannot convert the string to a object Id within the pipeline, you'll have to go though each document and convert it manually, using something like (you shouldnt be storing a mix match of types anyway, so it's probably worth updating in the long run):
how to convert string to numerical values in mongodb
as for does $lookup use index, If you look at the stats from this blog you'll see that indexes are used -
http://guyharrison.squarespace.com/blog/2016/7/4/join-performance-in-mongodb-32-using-lookup.html
Try casting your brands to ObjectIds before your population:
user_bookmarked.brands.map((brand) => return mongoose.Types.ObjectId(brand) )
But you really should consider storing them as refs instead, your model should look something like:
const user_bookmarked = new mongoose.Schema({
...
brands: [{type: mongoose.Schema.Types.ObjectId, ref: 'Brands'}],
...
})
This way they will be ObjectIds from the start.!
Regarding the second question this post explains it I think: join-performance-in-mongodb-32-using-lookup