How do I index two arrays in MongoDB?
-
db.hello.ensureIndex({"array1":1, "array2":1}) MongoDB does not allow that, because they say "it can get out of hand". However, I know that my arrays will never exceed length of 3. How can I hack MongoDB to allow indexing multiple arrays at once? When using a compound index, at most one of indexed values in any document can be an array. So if we have an index on {a: 1, b: 1}, the following documents are both fine: {a: [1, 2], b: 1} {a: 1, b: [1, 2]} This document, however, will fail to be inserted, with an error message "cannot index parallel arrays": {a: [1, 2], b: [1, 2]} The problem with indexing parallel arrays is that each value in the cartesian product of the compound keys would have to be indexed, which can get out of hand very quickly.
-
Answer:
The short answer to your question is; you don't. The only option available to you is to store the every unique pair as a single array element. So rather than : {a:[1,2], b[8,9]} you store {ab:[[1,8], [1,9], [2,8], [2,9]]} Obviously this has a few downsides so it really depends on your specific usecase whether or not this is an appropriate workaround. I do agree however that mongo shouldn't reject multiple array indexes just for idiot proofing. It's a good feature for small/low cardinality arrays.
TIMEX at Stack Overflow Visit the source
Related Q & A:
- How do I compare two tables with the same column definitions in different schemas?Best solution by Database Administrators
- How do I return two UITableViewCells?Best solution by Stack Overflow
- How do I bridge two different amps together?Best solution by ecoustics.com
- How do I put two pictures together vertically?Best solution by Yahoo! Answers
- How can I connect two monitors to my laptop?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.