0

My question is about the way MongoDB operates when querying MultiKey document.

Assuming I have these documents: { a: 1, b: 2, c: ['x','y','z'] }, { a:3, b: null, c: ['x','z'] }

My query is this: db.<collection>.find({ b: null, c: 'x'})

And my index is: db.<collection>.ensureIndex({ c: 1 })

My question is: For the query above (that asks for c AND b), how does MongoDB invokes the query? Does it 'see' that I have an index on c or does it try to only look for an index for both c AND b ?

refaelos
  • 7,927
  • 7
  • 36
  • 55
  • 1
    use .explain() method after find() and look at nscannedObjects and nscanned. it helps you to check usage of index – Disposer Dec 30 '14 at 17:51
  • Just did that... and nscannedObjects is the size of the collection. Why does it go through the entire collection just to get something by an indexed field. – refaelos Dec 30 '14 at 17:53
  • 1
    I made a test case for myself based on your docs and for searching in 2 docs, I got nscanned = 1, means it uses the indexing. look at cursor in .explain() and if it using the index, the cursor should be something like "BtreeCursor c_1" – Disposer Dec 30 '14 at 17:57
  • Cool. It works now. Probably it takes time to index large collections. – refaelos Dec 30 '14 at 18:00

1 Answers1

0

Thanks Disposer

The query just finds the index of c even if the statement includes c and b.

refaelos
  • 7,927
  • 7
  • 36
  • 55