查询MongoDB(使用Edge集合-最有效的方法吗?) [英] Querying MongoDB (Using Edge Collection - The most efficient way?)

查看:67
本文介绍了查询MongoDB(使用Edge集合-最有效的方法吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为下面的示例,我编写了用户,俱乐部和关注者"收藏集. 我想从著名俱乐部"后面的用户"集合中找到所有用户文档. 如何找到那些?哪种方法最快?

I've written Users, Clubs and Followers collections for the sake of an example the below. I want to find all user documents from the Users collection that are following "A famous club". How can I find those? and Which way is the fastest?

有关"的更多信息我想做什么-用户集合

{
    "_id": "1",
    "fullname": "Jared",
    "country": "USA"
}

俱乐部收藏

{
    "_id": "12",
    "name": "A famous club"
}

关注者收藏

{
    "_id": "159",
    "user_id": "1",
    "club_id": "12"
}

PS:我可以像下面这样用Mongoose获取文档.但是,创建具有150.000条记录的followers数组大约需要8秒钟.第二个find查询-使用followers数组查询-花费大约40秒.正常吗

PS: I can get the documents using Mongoose like the below way. However, creating followers array takes about 8 seconds with 150.000 records. And second find query -which is queried using followers array- takes about 40 seconds. Is it normal?

Clubs.find(
    { club_id: "12" },  
    '-_id user_id',      // select only one field to better perf.
    function(err, docs){ 

        var followers = [];
        docs.forEach(function(item){
            followers.push(item.user_id)
        })                

        Users.find(
            { _id:{ $in: followers } },
            function(error, users) {
                console.log(users) // RESULTS
        })
})

推荐答案

在MongoDB上没有符合条件的公式来操纵联接多对多关系.因此,我将集合合并为嵌入式文档,如下所示.但是在这种情况下,最重要的任务是创建索引.例如,如果您想通过followingClubs进行查询,则应使用Mongoose创建类似schema.index({ 'followingClubs._id':1 })的索引.如果要查询countryfollowingClubs,则应创建另一个索引,例如schema.index({ 'country':1, 'followingClubs._id':1 })

There is no an eligible formula to manipulate join many-to-many relation on MongoDB. So I combined collections as embedded documents like the below. But the most important taks in this case creating indexes. For instance if you want to query by followingClubs you should create an index like schema.index({ 'followingClubs._id':1 }) using Mongoose. And if you want to query country and followingClubs you should create another index like schema.index({ 'country':1, 'followingClubs._id':1 })

使用嵌入式文档时请注意: http://askasya.com/post/largeembeddedarrays

Pay attention when working with Embedded Documents: http://askasya.com/post/largeembeddedarrays

然后,您可以快速获取文档.我尝试使用这种方法仅花费1秒就获得了150.000条记录.对我来说足够了...

Then you can get your documents fastly. I've tried to get count of 150.000 records using this way it took only 1 second. It's enough for me...

ps:我们别忘了,在我的测试中,我的Users集合从未经历过任何数据碎片.因此,我的查询可能显示出良好的性能.尤其是followingClubs嵌入式文档数组.

ps: we musn't forget that in my tests my Users collection has never experienced any data fragmentation. Therefore my queries may demonstrated good performance. Especially, followingClubs array of embedded documents.

用户集合

{
    "_id": "1",
    "fullname": "Jared",
    "country": "USA",
    "followingClubs": [ {"_id": "12"} ]
}

俱乐部收藏

{
    "_id": "12",
    "name": "A famous club"
}

这篇关于查询MongoDB(使用Edge集合-最有效的方法吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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