Skip to content

[BUG] _id field falsely reported as non-unique on GET /v1/collections/{id} #316

@psofiterol

Description

@psofiterol

Observing that the indexes section of the endpoint above, returns this for a brand new collection:

[
  (v=2, key={'_id': 1}, name='_id_', unique=False),
  (v=2, key={'_updated': 1}, name='_updated_1', unique=False),
  (v=2, key={'_created': 1}, name='_created_1', unique=False)
]

Adding a new index with POST v1/collections/{id}/indexes , unique on purpose, results into this updated version:

[
  (v=2, key={'_id': 1}, name='_id_', unique=False),
  (v=2, key={'_updated': 1}, name='_updated_1', unique=False),
  (v=2, key={'_created': 1}, name='_created_1', unique=False),
  (v=2, key={'country_code': 1}, name='new_index_1', unique=True)
]

So the newly added index is picked up as unique, but the _id one is not. Checking this on MongoCompass (attached image). Sure enough, _id is reported as unique.

Image

How is this wired in nilDB? We can see the indexes retrieved from here. We go into mongodb territory - this should return a list of IndexDescriptionInfo which is annotated in the lib with:

The index information returned by the listIndexes command. https://www.mongodb.com/docs/manual/reference/command/listIndexes/#mongodb-dbcommand-dbcmd.listIndexes

Now going into mongosh and running the command from the page above, we get (for the same collection as earlier on MongoCompass):

stg_n1_nildb_data> db.runCommand({
...   listIndexes: "d935f706-3716-4649-8fb9-c7b41cae60b6",
...   cursor: { batchSize: 100 }
... })
{
  cursor: {
    id: Long('0'),
    ns: 'stg_n1_nildb_data.d935f706-3716-4649-8fb9-c7b41cae60b6',
    firstBatch: [
      { v: 2, key: { _id: 1 }, name: '_id_' },
      { v: 2, key: { _updated: 1 }, name: '_updated_1' },
      { v: 2, key: { _created: 1 }, name: '_created_1' },
      {
        v: 2,
        key: { country_code: 1 },
        name: 'new_index_1',
        unique: true
      }
    ]
  },
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1761828061, i: 1 }),
    signature: {
      hash: Binary.createFromBase64('e5Sh2+tOVYm9aLtTgsTcgf0caCk=', 0),
      keyId: Long('7528504678301040642')
    }
  },
  operationTime: Timestamp({ t: 1761828061, i: 1 })
}

And indeed, the _id index doesn't return a unique field here in contrast to the custom new_index added with POST v1/collections/{id}/indexes.

Noting that it definitely should be a unique field by default, per MongoDB docs:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions