MongoDB:通过嵌套键查找与顶级键查找
选项 1:
{
_id: ObjectId,
text: String,
author: {
id: ObjectId,
name: String,
email: String
}
}
选项 2:
{
_id: ObjectId,
text: String,
authorId: Id,
author: {
name: String,
email: String
}
}
我有一个类似于上面选项 1 的 Post 文档架构。有一个单独的“作者”集合,上面的作者 ID 字段正在引用该集合。此处重复了“姓名”和“电子邮件”;作者收藏中有更多作者信息。
我对 Post 集合的查询之一是查询某个作者 ID 的所有帖子。
就性能而言,是否最好选择选项 2,因为作者 ID 密钥位于文档的“顶层”?如果像选项 1 那样通过嵌套键搜索文档有什么区别吗?
Option 1:
{
_id: ObjectId,
text: String,
author: {
id: ObjectId,
name: String,
email: String
}
}
Option 2:
{
_id: ObjectId,
text: String,
authorId: Id,
author: {
name: String,
email: String
}
}
I have a schema for Post documents similar to option 1 above. There is a separate 'Author' collection, which the author ID fields above are referencing. The 'name' and 'email' have been duplicated here; there is more author information on the Author collection.
One of my queries on the Post collection will be querying for all posts by a certain author ID.
In terms of performance, is it best to go with Option 2 because the author ID key is at the 'top-level' of the document? Is there any difference if you search for documents by a nested key like in Option 1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,它不会产生显着的性能差异。 MongoDb 可以很好地索引“顶级”键或“嵌套键”。
MongoDb 可以在内部读取完整的 BSON 对象,从而有效地到达它的任何部分。
阅读此处:
http://www.mongodb.org/display/DOCS/BSON
您可以尝试创建a 2 测试集合,并在查询字段上创建索引。
性能将是相同的。
No, it won't make a significant performance difference. MongoDb can very well index a 'top-level' key or 'nested key'.
MongoDb can internally read full BSON object, hence reach any part of it efficiently.
Read here :
http://www.mongodb.org/display/DOCS/BSON
You can try creating a 2 test collections, and create indexes on the queried fields.
The performance will be the same.