mongodb查询中的嵌套文档是否存在性能差异?

时间:2012-11-15 10:05:18

标签: performance mongodb nested

如果我为用户名字段创建索引,哪个文档更适合查询特定用户?

嵌套的:

 {
        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'
        }
    }

1 个答案:

答案 0 :(得分:15)

没有区别 你正在做:

db.collection.findOne({"username":"zhengyi"});

db.collection.findOne({"account.username":"zhengyi"});

阅读嵌入式文档的dot notation

同样,索引使用点表示法到达文档内部:

db.collection.ensureIndex({"username": 1});

db.collection.ensureIndex({"account.username": 1});