DaniKipod
(Daniel Khodus)
1
I have these valdation rules for my collection:
"$jsonSchema": {
"bsonType": "object",
"required": ["CCF_CNAME", "CCF_NPARS"],
"properties": {
"CCF_CNAME": {
"bsonType": "string",
"uniqueItems": true,
"description": "CCF_CNAME must be a unique string."
},
"CCF_NPARS": {
"bsonType": "int",
"description": "CCF_NPARS must be an integer."
}
}
}
}
but for some reason when I try to insert a document with a CCF_CNAME string that already exists, I don’t get an error…
For example I insert these two rows:
“POWER_ON” 1
“POWER_ON” 2
the string is the same, but no error. I ran the command
db.getCollectionInfos( { name: "ccf" } )[0].options.validator
and I got my validation correctly, so that’s not the problem. Also trying to use a CCF_NPARS value that is a float for example does give an error.
What am I missing?
Katya
(Katya Kamenieva)
2
uniqueItems
is a property of an array field; it checks that elements of the array are unique, that it’s a set. Schema validation only checks a single document that you are inserting/updating, it doesn’t compare different documents in a collection. To ensure uniqueness of the field value across documents, you can use unique indexes
DaniKipod
(Daniel Khodus)
3
Thank you for your reply!
Is there a way to somehow define unique columns in the json schema?