23

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

1 Answer 1

21

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});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.