Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-808

Db.createIndex will sometimes return cryptic, incorrect error messages

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.2.10
    • Affects Version/s: 2.2.9
    • Component/s: MongoDB 3.2
    • Labels:
    • Environment:
      Tested against: 3.2.6

      Sometimes during a `Db.createIndex` operation, the server will respond with a valid error to indicate that `createIndex` operation has failed.

      Testcase: https://github.com/suprememoocow/node-mongodb-native-create-index-error/blob/master/test.js

      For example, in the test-case where the server responds with:

      ```
      code: 85
      message: Index with pattern: ... already exists with different options
      ```

      Instead of returning this information to the server, the server falls-back to attempting an insert into the `system.indexes` collection.

      If the key for the index contains any period characters (which are valid for a the key, but not as a document for insertion into a collection) the insert will fail, leading to a situation where the user is presented with a cryptic error message instead of the original (understandable error).

      So, instead of getting an error like the one I presented above, the user will receive one like:
      ```
      Error: key a.one must not contain '.'
      at Error (native)
      at serializeInto (/../node-mongodb-native-create-index-error/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/parser/serializer.js:731:19)
      at serializeObject (/../node-mongodb-native-create-index-error/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/parser/serializer.js:294:18)
      at serializeInto (/../node-mongodb-native-create-index-error/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/parser/serializer.js:754:17)
      at serializeObject (/../node-mongodb-native-create-index-error/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/parser/serializer.js:294:18)
      at serializeInto (/../node-mongodb-native-create-index-error/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/parser/serializer.js:596:17)
      at serializeObject (/../node-mongodb-native-create-index-error/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/parser/serializer.js:294:18)
      at serializeInto (/../node-mongodb-native-create-index-error/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/parser/serializer.js:754:17)
      at serialize (/../node-mongodb-native-create-index-error/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/bson.js:49:27)
      at Query.toBin (/../node-mongodb-native-create-index-error/node_modules/mongodb/node_modules/mongodb-core/lib/connection/commands.js:143:25)
      ```

      While this example may seem fairly contrived, it can easily happen in a more natural manner, especially when using mongoose (as we are). I would have saved a fair deal of time had I been able to access the original error instead of the incorrect one.

            Assignee:
            Unassigned Unassigned
            Reporter:
            andrewn Andrew Newdigate
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: