MongoDB unique compound index on _id and nested field not working

31 Views Asked by At

I have a document with the following structure

{
  "_id": 1,
  "list":[
     {
      "_id": {
        "$oid": "64e4e7dcb7affd390938c40c"
      },
      "some_field": null,
      "needed_field": "string",
      "last_updated": {
        "$date": "2023-08-22T16:52:44.657Z"
      }
    }
  ]
}

I want to create a unique compound index on the combination of _id of the document and the link field in paths collection.create_index([('_id', 1), ('list.needed_field', 1)], unique=True, background=True) And running db.collection.getIndexes() returns the following

[
  {
    "v": 2,
    "key": {
      "_id": 1
    },
    "name": "_id_"
  },
  {
    "v": 2,
    "key": {
      "_id": 1,
      "list.needed_field": 1
    },
    "name": "unique_need_field",
    "background": true,
    "unique": true
  }
]

But mongo still allows me to insert the same "needed_field" values What could be going wrong here?

0

There are 0 best solutions below