Is there any performance difference for nested document in mongodb query

后端 未结 1 1465
生来不讨喜
生来不讨喜 2021-02-18 16:30

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

The nested one:

 {
        account:{
        username:\'z         


        
相关标签:
1条回答
  • 2021-02-18 17:12

    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});
    
    0 讨论(0)
提交回复
热议问题