更改mongoDB聚合内的字段类型,并且$ lookup是否利用字段上的索引? [英] Change type of field inside mongoDB aggregation and does $lookup utilises index on fields or not?

查看:847
本文介绍了更改mongoDB聚合内的字段类型,并且$ lookup是否利用字段上的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用$ lookup在mongodb中执行联接,现在我在这里遇到了问题. 我有两个集合,第一个集合包含用户所有书签品牌,第二个集合包含有关品牌的所有详细信息.现在,我试图返回用户添加书签的所有品牌详细信息.

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 contains all details about the brands.Now i am trying to return all brands details bookmarked by the user.

用户已收藏收藏夹

{"mobile_no": "8971740148", "brands": ["5829c1df334d40e20e1d1c19", "5829c1df334d40e20e1d1c20", "5829c1df334d40e20e1d1c21"]}

品牌收藏

{"_id": ObjectId("5829c1df334d40e20e1d1c19"), "brand_name": "Versace"}
{"_id": ObjectId("5829c1df334d40e20e1d1c20"), "brand_name": "Lee Cooper"}
{"_id": ObjectId("5829c1df334d40e20e1d1c21"), "brand_name": "Levis"}

下面给出了我的聚合管道代码

My aggregation pipeline code is given below

                { $match: { mobile_no: mobile_no }},
                { $unwind: { path: "$brands", includeArrayIndex: "brandsposition"}},
                { $lookup: {from: "brands",localField: "brands",foreignField: "_id",as: "user_bookmarks"}},

现在我面临的问题是上面的代码没有返回任何内容,因为我将品牌ID以字符串的形式存储在我的user_bookmarked集合中,而不是以ObjectId的形式存储,因此没有返回任何内容.现在任何人都可以告诉我如何更改聚合查询中的字段类型.

Now the problem i am facing is that above code doesn't return anything as i am storing brands ids as string in my user_bookmarked collection but not as ObjectId so nothing is being returned. Now can anyone please tell me how can i change field type inside aggregation query.

我想问的第二件事是使用$ lookup时,请告诉我,然后mongodb是否利用foreign_field上的索引.因为我使用 explain:true 在聚合管道上方运行,但是我没有发现上述查询使用的任何索引,所以我将其返回给输出.

Second thing i want to ask please tell me when using $lookup then does mongodb utilises index on foreign_field or not. Because i ran above aggregation pipeline with explain: true but i don't found any index that was utilised by above query i got this returned by the output.

db.user_bookmarked.runCommand('aggregate', {pipeline: [{ $match: { mobile_no: mobile_no }},
            { $unwind: { path: "$brands", includeArrayIndex: "brandsposition"}},
            { $lookup: {from: "brands",localField: "brands",foreignField: "_id",as: "user_bookmarks"}}], explain: true})
{
        "waitedMS" : NumberLong(0),
        "stages" : [
                {
                        "$cursor" : {
                                "query" : {
                                        "mobile_no" : "8971740148"
                                },
                                "queryPlanner" : {
                                        "plannerVersion" : 1,
                                        "namespace" : "test.restaurants",
                                        "indexFilterSet" : false,
                                        "parsedQuery" : {
                                                "mobile_no" : {
                                                        "$eq" : "8971740148"
                                                }
                                        },
                                        "winningPlan" : {
                                                "stage" : "COLLSCAN",
                                                "filter" : {
                                                        "mobile_no" : {
                                                                "$eq" : "8971740148"
                                                        }
                                                },
                                                "direction" : "forward"
                                        },
                                        "rejectedPlans" : [ ]
                                }
                        }
                },
                {
                        "$unwind" : {
                                "path" : "$brands",
                                "includeArrayIndex" : "brandsposition"
                        }
                },
                {
                        "$lookup" : {
                                "from" : "brands",
                                "as" : "user_bookmarks",
                                "localField" : "brands",
                                "foreignField" : "_id"
                        }
                }
        ],
        "ok" : 1
}

现在任何人都可以从这里帮助我,我已经搜索了这两个东西,这是如何更改聚合内的字段类型,并且$ lookup利用索引,但是还没有发现有用的东西,请帮助我真的很有意义.

Now can anyone please help me out of here i have searched about both of this thing which is how to change type of field inside aggregation and does $lookup utilises indexes but Haven't found anything useful please help me out of here guys it would be really appreciable.

推荐答案

您无法将字符串转换为管道中的对象ID,您将不得不遍历每个文档并使用类似方法手动将其转换(您不应该无论如何都要存储类型的混合匹配,因此从长远来看可能值得更新):

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):

如何在mongodb中将字符串转换为数值 a>

$ lookup是否使用索引,如果您查看此博客的统计信息,就会发现使用了索引-

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

这篇关于更改mongoDB聚合内的字段类型,并且$ lookup是否利用字段上的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆