Is there any performance difference for nested document in mongodb query

梦想与她 提交于 2019-12-05 12:57:25

问题


If I create an index for username field, which document is better for query a specific user?

The nested one:

 {
        account:{
        username:'zhengyi',  
        passwd:'zhengyi',
        friends:[]
        }
        dev:{
            os:'iOS',
            ver:'6.0',
            hw:'iPhone2,1'
        },
        app:{
            ver:'1.0',
            pver:'1.0'
        }
    }

The unnested one:

  {    
        username:'zhengyi',
        passwd:'zhengyi',
        friends:[],
        dev:{
            os:'iOS',
            ver:'6.0',
            hw:'iPhone2,1'
        },
        app:{
            ver:'1.0',
            pver:'1.0'
        }
    }

回答1:


It doesn't make a difference
You're either doing:

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

or

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});

Or

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


来源:https://stackoverflow.com/questions/13395188/is-there-any-performance-difference-for-nested-document-in-mongodb-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!