3
  1. I've set up an Azure Cosmos DB that exposes the MongoDB API.
  2. I connect to the DB with the mongo shell.
  3. I create a new db sample with use sample.
  4. I create a simple collection sample with db.sample.insert({})
  5. I then try to run the following query:
db.sample.findOneAndUpdate({ foo: {"$eq":1}},{ "$set": { "bar":1}},{"sort": { "foo": 1, "bar": 1 }})

When I run this against a normal MongoDB 3.2.0 instance, the query completes successfully (and matches 0 results). However, on CosmosDB, the following stacktrace is produced:

2019-01-31T13:46:49.740+0100 E QUERY    [thread1] Error: findAndModifyFailed failed: {
    "_t" : "OKMongoResponse",
    "ok" : 0,
    "code" : 2,
    "errmsg" : "Message: {\"Errors\":[\"Encountered exception while executing function. Exception = Error: {\\\"Errors\\\":[\\\"The order by query does not have a corresponding composite index that it can be served from.\\\"]}\\r\\nStack trace: Error: {\\\"Errors\\\":[\\\"The order by query does not have a corresponding composite index that it can be served from.\\\"]}\\n   at queryCallback (__.sys.commonUpdate_BsonSchema.js:3835:26)\\n   at Anonymous function (__.sys.commonUpdate_BsonSchema.js:607:29)\"]}\r\nActivityId: ff7b4476-0000-0000-0000-000000000000, Request URI: /apps/c2de8f1c-dee1-4b6c-8543-479a695c3bb2/services/12da6069-653a-45be-bfa1-4394a0798877/partitions/2814e64e-3d06-4705-a346-cbbfc822a49a/replicas/131934057017796979p/, RequestStats: \r\nRequestStartTime: 2019-01-31T12:46:49.6846466Z, Number of regions attempted: 1\r\n, SDK: Microsoft.Azure.Documents.Common/2.1.0.0",
    "$err" : "Message: {\"Errors\":[\"Encountered exception while executing function. Exception = Error: {\\\"Errors\\\":[\\\"The order by query does not have a corresponding composite index that it can be served from.\\\"]}\\r\\nStack trace: Error: {\\\"Errors\\\":[\\\"The order by query does not have a corresponding composite index that it can be served from.\\\"]}\\n   at queryCallback (__.sys.commonUpdate_BsonSchema.js:3835:26)\\n   at Anonymous function (__.sys.commonUpdate_BsonSchema.js:607:29)\"]}\r\nActivityId: ff7b4476-0000-0000-0000-000000000000, Request URI: /apps/c2de8f1c-dee1-4b6c-8543-479a695c3bb2/services/12da6069-653a-45be-bfa1-4394a0798877/partitions/2814e64e-3d06-4705-a346-cbbfc822a49a/replicas/131934057017796979p/, RequestStats: \r\nRequestStartTime: 2019-01-31T12:46:49.6846466Z, Number of regions attempted: 1\r\n, SDK: Microsoft.Azure.Documents.Common/2.1.0.0"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCollection.prototype.findAndModify@src/mongo/shell/collection.js:757:1
DBCollection.prototype.findOneAndUpdate@src/mongo/shell/crud_api.js:785:12
@(shell):1:1

Are there some additional query restrictions that exist in CosmosDB that don't exist in normal MongoDB instances? Is this just a CosmosDB bug? What is the appropreate way to go around reporting such bugs?

Stennie
  • 63,885
  • 14
  • 149
  • 175
Jacob Horbulyk
  • 2,366
  • 5
  • 22
  • 34
  • 1
    Does your `findOneAndUpdate()` call work without the `sort`? Or with a single-property `sort`? Compound indexes are not currently supported (you can vote on the Uservoice request, [here](https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/33544174-compound-indexes)). – David Makogon Jan 31 '19 at 13:03
  • @DavidMakogon It works without the sort & with single property sorts. I think you've correctly identified the Azure limitation. If you put your remarks in a SO answer, I will accept it as correct. – Jacob Horbulyk Jan 31 '19 at 13:06

1 Answers1

2

You're not seeing a bug; currently Cosmos DB's MongoDB API does not support compound indexes. Your findOneAndUpdate() should work find either without the sort or with a single-property sort.

Note: There is a UserVoice request posted (here) that you can vote on, and which has a status update from the product team; this might be helpful for you.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • Two Follow-up questions: 1. Is there any official Micorsoft documentation that describes this limitation? 2. Would one expect to encounter this limitation for any sort statement that includes more than one field? – Jacob Horbulyk Jan 31 '19 at 13:19
  • 1
    The team has a published compatibility doc ([here](https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb-feature-support)). I don't see mention of the `sort` limitation, but feel free to contribute to it (the page supports editing via github). – David Makogon Jan 31 '19 at 13:20