Search code examples
mongodbindexinggeospatialmongodb-querymultikey

geospatial index with another multikey index... Any solutions?


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"}                    
        ],

    }

Solution

  • 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.