0

For example: If I had a db collection called Stores, and each store document has a list of the items they sell, and stores generally share items, then how would mongodb build an index on that? Would it build a btree index on all possible items and then on each leaf of that tree (each item) will reference the documents which contain it?

Background: I'm trying to perform queries like this using an index:

db.store.find({merchandise:{$exists:true}}) // where 'merchandise' is a list
db.store.find()[merchandise].count()

would an index on 'merchandise' help me? If not, is my only option creating a separate meta field on 'merchandise' size, and index that?

Schema:

{ _id: 123456,
  name: Macys
  merchandise: [ 248651234564, 54862101248, 12450184, 1256001456 ]
}
2
  • Can you share the schema of your document? Commented Sep 3, 2013 at 19:48
  • When you want to know if a certain query is aided by an index or not, .explain() is your friend. Commented Sep 3, 2013 at 20:45

2 Answers 2

1

From your document sample if you build your index on merchandise it will be multikey index and that index will be on every item on the array. See Multikey Indexes section in here.

If merchandise is an array of subdocuments, indexing over merchandise will put the index on all field of subdocument in the array. With index you can make queries like db.store.find("merchandise":248651234564) and it will retrieve all document having merchandise 248651234564

For getting count of merchandise, you can get only get the size of merchandise field of one document like db.store.find()[index].merchandise.length. So creating a seperate field on merchandise size and indexing is a feasible option, if you want to run queries based on merchandise size.

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

1

If you index a field that contains an array, MongoDB indexes each value in the array separately, in a multikey index. When you have 4 documents inside an array, each will act as a key in the index and point to the mentioned document(s).

You can use multikey indexes to index fields within objects embedded in arrays. That means, in your array, you can index a specific field in each document. For example: stuffs.thing : 1.

Read more about Multikey Indexes

Whether you need these indexes would depend on:

  • How many queries rely on that specific field?
  • How many updates, inserts hit that specific field (array)?
  • How many items will that array contain?
  • ...

Remember that indexes slow writes as they need to be updated as well. I'd consider an explain on my queries to measure performance.

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.