mongodb查询中的嵌套文档是否有任何性能差异 [英] Is there any performance difference for nested document in mongodb query
本文介绍了mongodb查询中的嵌套文档是否有任何性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我为用户名字段创建索引,哪个文档更适合查询特定用户?
If I create an index for username field, which document is better for query a specific user?
嵌套的一个:
{
account:{
username:'zhengyi',
passwd:'zhengyi',
friends:[]
}
dev:{
os:'iOS',
ver:'6.0',
hw:'iPhone2,1'
},
app:{
ver:'1.0',
pver:'1.0'
}
}
未嵌套的一个:
{
username:'zhengyi',
passwd:'zhengyi',
friends:[],
dev:{
os:'iOS',
ver:'6.0',
hw:'iPhone2,1'
},
app:{
ver:'1.0',
pver:'1.0'
}
}
推荐答案
没什么区别
您正在做的事:
It doesn't make a difference
You're either doing:
db.collection.findOne({"username":"zhengyi"});
或
db.collection.findOne({"account.username":"zhengyi"});
在点符号上进行阅读用于嵌入式文档
Read up on the dot notation for embedded documents
类似地,索引使用点表示法到达文档内部:
Similarly, indexes use the dot notation to reach inside a document:
db.collection.ensureIndex({"username": 1});
或
db.collection.ensureIndex({"account.username": 1});
这篇关于mongodb查询中的嵌套文档是否有任何性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文