0

I have a collection like below. I want to index "location" and "product_list.id". MongoDB seems to permit only single multi key index in a document. Any work around possible?

  {
    "location":[62.99932,71.23424],
    "product_list":[
        {"id":"wf2r34f34ff33", "price": "87.99"},
        {"id":"f334r3rff43ff", "price": "21.00"},
        {"id":"wf2r34f34ffef", "price": "87.99"}                    
        ],

    }
Joe
  • 3
  • 1

1 Answers1

0

True, you can only index on a a single array type of field within a single compound index of a collection, but you seem to be talking about "geo-spatial" queries which are something a little different. There is nothing wrong with this at all:

db.collection.ensureIndex({ "location": "2d", "product_list": 1 })

That is a perfectly valid form for a compound index.

So it's looks like an array, but in this case MongoDB treats it differently.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • Great! Thanks Niel! Can you point me to any documentation or a blog which gives a detailed explanation of the same. I will try this. – Joe May 26 '14 at 05:39
  • There are a few statements in the documentation like [this](http://docs.mongodb.org/manual/reference/operator/query/near/#considerations) Which says you cannot combine a geo-spatial query with another special such as text. But there is nothing wrong with adding another simple key in compound. So something that results in a multikey index in compound is not a problem. The simple test is that the index creation does not cause an error and in fact can be used in a query. If you tried `location:1, product_list: 1` for example, you would get an error. – Neil Lunn May 26 '14 at 05:48