Change type of field inside mongoDB aggregation and does $lookup utilises index on fields or not?

荒凉一梦 提交于 2019-11-28 11:50:47
Kevin Smith

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!