- You're using Azure CosmosDB (I know, I don't like it too)
- Have a Mongoose query with a sort option against a ..
- Field that inside a sub document.
`{ sort: req.query.order || '-metaData.inserted_at' }`
. The metaData.inserted_at field is just a date field. MetaData is just a plain object that has a couple of date fields tracking updates, deletes and such.
So when you submit the query it spits out the Error=2 response.CAUTION: Azure CosmosDB has a emulator isn't really helpful here. It will probably point you in a different direction. In my case, I was able to replicate the error and found a fix in where I 'unchecked' the Provision Throughput option in creating the database. That didn't solve the problem on the server.
In fixing this, you have two options:
- No sorting in your code.
- Create the index
db.getCollection('collectionName').getIndexes();
db.getCollection('collectionName').createIndex({'metaData.inserted_at':-1});
// shorter version
db.collectionName.createIndex({'metaData.inserted_at':-1});
It will be pain if you created an index that's wrong because you have to delete and create it again.References:
- https://docs.mongodb.com/manual/tutorial/manage-indexes/#modify-an-index
- https://docs.microsoft.com/en-us/azure/cosmos-db/index-overview