I define a schema like
{
'info' : {
'type' : 'dict',
'unique' : True,
'schema' : {
'propertyA' : {'type':'string'},
'propertyB' : {'type':'string'},
'propertyC' : {'type':'string'}
}
},
'others' : {'type':'string'}
}
Then, I post the following document twice, the first time it returns OK, and the second time it returns "not unique error".
[
{
"info" : {
"propertyA" : "a",
"propertyB" : "b",
"propertyC" : "c"
},
"others" : "other things"
}
]
But when I post a document list as follows:
[
{
"info" : {
"propertyA" : "a",
"propertyB" : "b",
"propertyC" : "c"
},
"others" : "other things"
},
{
"info" : {
"propertyA" : "a",
"propertyB" : "b",
"propertyC" : "c"
},
"others" : "other things"
}
]
The both documents are inserted to database.
Why they have different result?
That happens because in the second scenario both documents are going to be stored with one loopback to the database, taking advantage of MongoDB bulk insert capabilities. So when the second document is validated against the database there's no duplicate to be found yet.
In the first scenario you're performing two separate save operations, so the second will match the duplicate.