Unique index in MongoDB

好久不见. 提交于 2019-12-10 19:25:54

问题


Can someone please help me with creation of unique index @ mongoDB for the following condition ?

Suppose I have a schema like this, and I want a unique compound index on 1.path 2.verb 3."switches.name" 4."switches.value"

{
  "path": "somepath",
  "verb": "GET",
  "switches": [
    {
      "name": "id",
      "value":"123"
    },
    {
      "name": "age",
      "value":"25"
    }
  ]
}

So if I try to insert

{
  "path": "somepath",
  "verb": "GET",
  "switches": [
    {
      "name": "id",
      "value":"123"
    },
    {
      "name": "age",
      "value":"25"
    }
  ]
}

I should get duplicate error, but if I insert

{
  "path": "somepath",
  "verb": "GET",
  "switches": [
    {
      "name": "id",
      "value":"123"
    },
    {
      "name": "age",
      "value":"24"
    }
  ]
}

or

{
  "path": "somepath",
  "verb": "GET",
  "switches": [
    {
      "name": "id",
      "value":"123"
    },
    {
      "name": "age",
      "value":"25"
    },
    {
      "name": "foo",
      "value":"bar"
    }
  ]
}

I shouldn't get any error.

Basically, I should be allowed to insert documents with distinct array "switches".


回答1:


I am afraid what you want to achieve cannot be by your current schema.

First you have to understand how array on indexes work: https://docs.mongodb.com/manual/core/index-multikey/#multikey-indexes

To index a field that holds an array value, MongoDB creates an index key for each element in the array.

This suggests that arrays are not indexed as single objects, but are unwinded into multiple objects.

To achieve a similar result to what you want, you may consider using object property maps instead:

{
    "path" : "somepath", 
    "verb" : "GET", 
    "switches" : {
        "id": "123",
        "age": "25"
    }
}

And create as unique index as usual:

db.yourCollection.createIndex({"path": 1, "verb": 1, "switches": 1}, {"unique": true});

However, this is generally undesirable because querying for the key is non-trivial
(Also read about this here).

So alternatively, you can wrap the array within another object instead:

{
    "path" : "somepath", 
    "verb" : "GET", 
    "switches" : {
        "options": [
            {
                "name" : "id", 
                "value" : "123"
            }, 
            {
                "name" : "age", 
                "value" : "25"
            }
        ]
    }
}

With the same index:

db.yourCollection.createIndex({"path": 1, "verb": 1, "switches": 1}, {"unique": true});

If you plan to execute queries on the switches.options array, this would be the more desirable solution.




回答2:


yourDatabase.yourCollection.ensureIndex({"path": 1, "verb": 1, "switches": 1}, {"unique": true})

you can use ensureCollection() or createCollection as you like. https://docs.mongodb.com/manual/core/index-unique/



来源:https://stackoverflow.com/questions/38044677/unique-index-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!