I have a collection where i need to have a unique item of arrays.
The items represents a SSDP matching. E.g:
{
"_id":"63271ed8e47cf61cb98732c4",
"headers":[
"gwid.phoscon.de"
],
"nt":"urn:schemas-upnp-org:device:basic:1",
"description":"Phoscon Gateway",
"timestamps":{
"created":1663508184610,
"updated":null,
"announced":null
},
"usn": null
}
Now when i add a another dataset, with the same header values & nt + usn value, the driver should throw a duplicate key error (11000).
Only when the three criterias / values are the same in the new dataset it shuld throw the duplicate key error. Similiar to this question: Mongodb unique array values combination index .When one value does differ, it should be added to the collection.
How can i do this?
I tried to set a unique index to "usn" & "nt", but this does not work. Default value for "usn" is null, so when no "usn" is set, only one item can be added to the database.
Something like this:
collection.createIndex({
"headers":1,
"usn":1,
"nt":1
},{
"unique":true
});
But the example above does not care for the array values. As soon as "usn" or "nt" allready exists, the result it duplicate key error.
I need a "combined" unique index for the the three fields "headers", "nt" & "usn".
Is that possible?