What is the preferred way to create index

I want to know if there is any difference in performance (for read and write) in the following scenarios

e.g Documents

{ "price" : 10, "qty" : 2 }

{ "item" : { "price" : 1, "qty" : 23 } }

I want to have an index on the “price” and “qty” field. What would be the better way to structure this document or they are exactly the same ?


Sorry, but this is a very imprecise and incomplete question. Please post the two index creation commands you’re asking about along with your sample documents.

Also notice that we’ll get more into index handling and how to tune indexes next week.

Sorry about that. What I wanted to ask was, how should I structure my document
If I structure my document in the first way, following will be the index creation command.

db.myColl.createIndex( {“price”: 1 } )
db.myColl.createIndex( {“qty”: 1 } )

and if I structure my document the second way then the commands would be
db.myColl.createIndex( {“item.price”: 1 } )
db.myColl.createIndex( {“item.qty”: 1 } )

So is there going to be any difference in performance ?

or a more general question would be should the fields that are to be indexed be kept at the root level for maximum performance ?

@ Sharang_58264

Thanks-- that clarifies what you’re asking quite well.

There is no difference that is documented in performance – both of those will work just fine. And whether the fields are at the root of the document or not does not impact performance of the index – although of course if you were using only part of the index and retrieving multiple documents, then the schema structure would impact the overall query performance. But the index is equally efficient either way. HTH.