简体   繁体   English

MongoDB嵌入式vs数组子文档性能

[英]MongoDB embedded vs array sub document performance

Given the below competing schemas with up to 100,000 friends I'm interested in finding the most efficient for my needs. 鉴于以下竞争模式与多达100,000个朋友,我有兴趣找到最有效的我的需求。

Doc1 (Index on user_id) Doc1(user_id索引)

{
"_id" : "…",
"user_id" : "1",
friends : {
    "2" : {
        "id" : "2",
        "mutuals" : 3
    }
     "3" : {
         "id" : "3",
         "mutuals": "1"
    }
   "4" : {
         "id" : "4",
         "mutuals": "5"
    }
}
}

Doc2 (Compound multi key index on user_id & friends.id) Doc2(user_id和friends.id上的复合多键索引)

{
"_id" : "…",
"user_id" : "1",
friends : [
   {
        "id" : "2",
        "mutuals" : 3
    },
    {
         "id" : "3",
         "mutuals": "1"
    },
   {
         "id" : "4",
         "mutuals": "5"
    }
]}

I can't seem to find any information on the efficiency of the sub field retrieval. 我似乎无法找到有关子字段检索效率的任何信息。 I know that mongo implements data internally as BSON, so I'm wondering whether that means a projection lookup is a binary O(log n)? 我知道mongo在内部将数据实现为BSON,所以我想知道这是否意味着投影查找是二进制O(log n)?

Specifically, given a user_id to find whether a friend with friend_id exists, how would the two different queries on each schema compare? 具体来说,给定user_id以查找是否存在具有friend_id的朋友,每个模式上的两个不同查询将如何比较? (Assuming the above indexes) Note that it doesn't really matter what's returned, only that not null is returned if the friend exists. (假设上面的索引)请注意,返回的内容并不重要,只有在朋友存在时才返回非null。

Doc1col.find({user_id : "…"}, {"friends.friend_id"})
Doc2col.find({user_id : "…", "friends.id" : "friend_id"}, {"_id":1})

Also of interest is how the $set modifier works. 同样令人感兴趣的是$ set修饰符的工作原理。 For schema 1,given the query Doc1col.update({user_id : "…"}, {"$set" : {"friends.friend_id.mutuals" : 5}) , how does the lookup on the friends.friend_id work? 对于模式1,给定查询Doc1col.update({user_id : "…"}, {"$set" : {"friends.friend_id.mutuals" : 5}) ,friends.friend_id上的查找如何工作? Is this a O(log n) operation (where n is the number of friends)? 这是一个O(log n)操作(其中n是朋友的数量)?

For schema 2, how would the query Doc2col.update({user_id : "…", "friends.id" : "friend_id"}, {"$set": {"friends.$.mutuals" : 5}) compare to that of the above? 对于模式2,查询Doc2col.update({user_id : "…", "friends.id" : "friend_id"}, {"$set": {"friends.$.mutuals" : 5})比较以上的?

doc1 is preferable if one's primary requirements is to present data to the ui in a nice manageable package. 如果一个人的主要要求是在一个漂亮的可管理包中向ui提供数据,那么doc1更受欢迎。 its simple to filter only the desired data using a projection {}, {friends.2 : 1} 使用投影{}, {friends.2 : 1}只过滤所需数据很简单

doc2 is your strongest match since your use case does not care about the result Note that it doesn't really matter what's returned and indexing will speed up the fetch. doc2是你最强的匹配,因为你的用例并不关心结果请注意,返回的内容并不重要 ,索引会加快获取速度。

on top of that doc2 permits the much cleaner syntax 在那个doc2之上允许更清晰的语法

db.doc2.findOne({user_id: 1, friends.id : 2} )

versus

db.doc1.findOne({ $and : [{ user_id: 1 }, { "friends.2" : {$exists: true} }] })

on a final note, however, one can create a sparse index on doc1 (and use $exists) but your possibility of 100,000 friends -- each friend needed a sparse index -- makes that absurd. 然而,最后一点,人们可以在doc1上创建一个稀疏索引 (并使用$ exists),但你有10万朋友的可能性 - 每个朋友需要一个稀疏索引 - 这使得这很荒谬。 opposed to a reasonable number of entries say demographics gender [male,female], agegroups [0-10,11-16,25-30,..] or more impt things [gin, whisky, vodka, ... ] 反对合理数量的条目说人口统计学性别[男性,女性],年龄组[0-10,11-16,25-30,...]或更多的东西[杜松子酒,威士忌,伏特加,......]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将现有的子文档推送到 MongoDB 中的嵌入式数组中? - How to push an existing sub-document into an embedded array in MongoDB? MongoDB查询数组中嵌入式文档中的字段 - MongoDB query for field in embedded document in array 如何在MongoDb中的嵌入式文档中删除数组上的“属性”? - How to remove “attribute” on array in embedded document in MongoDb? 如何在 MongoDB 中的数组中查询单个嵌入文档? - How to query a single embedded document in an array in MongoDB? mongodb查找单个嵌入式文档作为对象而不是数组 - mongodb find single embedded document as an object not as an array 重命名MongoDB中数组中的嵌入式文档中的字段不起作用 - Renaming a field in an embedded Document in an Array in MongoDB not working MongoDB将字段添加到数组中的嵌入文档 - MongoDB add a field to a embedded document in an array 如何在 MongoDB 中的数组中获取嵌入文档(使用 Mongoose) - How to get embedded document in an array in MongoDB (with Mongoose) MongoDB:更新子文档中的特定数组元素 - MongoDB: Updating A specific array element in a sub document MongoDB 罗盘:如何过滤 MongoDB 文档中的嵌入式数组 object - MongoDB Compass: How to filter embedded array object in the MongoDB document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM