2

In mongodb I want to define a unique constraint on an inner array property but not within all documents. Can I do this using index constraints?

These two documents should be allowed:

{ name:'test1', properties: [ { key : 'key1', val: 'val1'}]}
{ name:'test2', properties: [ { key : 'key1', val: 'val2'}]}

What should not be allowed is having a document that has the same property key twice:

{ name:'test1', properties: [ { key : 'key1', val: 'val1'}, { key : 'key1', val: 'some other value'}]} // should fail
{ name:'test2', properties: [ { key : 'key1', val: 'val2'}, { key : 'key2', val: 'some other value'}]} // should be okay

any possibility to assert this on a database level?

styvane
  • 59,869
  • 19
  • 150
  • 156
MaxM
  • 625
  • 7
  • 13

0 Answers0