Details
-
Bug
-
Resolution: Won't Fix
-
Major - P3
-
None
-
None
-
None
-
None
Description
I think I found a bug in the unique index thing. Sometimes the unique index works, sometimes it doesn't. I was writing a test for it and when I do:
// create non-unique index, add two elements with the same value
$x = $c->ensureIndex(array('x'=>1), !$unique);
$c->insert(array('x'=>0));
$c->insert(array('x'=>0));
// count is 2
var_dump($c->count());
// create unique index, add two elements with the same value
$c->ensureIndex(array('z'=>1), $unique);
$c->insert(array('z'=>0));
$c->insert(array('z'=>0));
// I was expecting 3
var_dump($c->count());
...but it's 4. Okay, I thought, so maybe the theory is that the
{'x' : 0}elements implicitly have the same 'z' value, so it's creating a normal index for 'z', even though I'm asking for a unique index. This was supported by the fact that this worked:
// same as above, except setting the 'z' field
$x = $c->ensureIndex(array('x'=>1), !$unique);
$c->insert(array('x'=>0, 'z'=>1));
$c->insert(array('x'=>0, 'z'=>4));
// count is 2
var_dump($c->count());
$c->ensureIndex(array('z'=>1), $unique);
$c->insert(array('z'=>0));
$c->insert(array('z'=>0));
// count is 3
var_dump($c->count());
So then I added a test creating a third index, to test my theory:
// create a unique index on 'y', which none of the elements have set
$c->ensureIndex(array('y'=>1), $unique);
$c->insert(array('y'=>0));
$c->insert(array('y'=>0));
// I was expecting 5, as the unique failed to hold before, but it was 4!
var_dump($c->count());
So, I have no idea what's going on. I'm guessing it's a bug with the unique index code. Database output for running the whole test:
Tue Apr 21 10:18:46 connection accepted from 127.0.0.1:50082
Tue Apr 21 10:18:46 CMD: drop foo.bar
Tue Apr 21 10:18:46 d->nIndexes was 2
Tue Apr 21 10:18:46 info: creating collection foo.bar on add index
Tue Apr 21 10:18:46 building new index on
for foo.bar...done for 0 records
Tue Apr 21 10:18:46 building new index on
for foo.bar...done for 2 records
Tue Apr 21 10:18:46 User Exception E11000 duplicate key error
Tue Apr 21 10:18:46 foo.bar Caught Assertion insert, continuing
Tue Apr 21 10:18:46 building new index on
for foo.bar...Tue Apr 21 10:18:46 User Exception E11000 duplicate key error
d->nIndexes was 3
Tue Apr 21 10:18:46 foo.system.indexes Caught Assertion insert, continuing
Tue Apr 21 10:18:46 User Exception E11000 duplicate key error
Tue Apr 21 10:18:46 foo.bar Caught Assertion insert, continuing
Tue Apr 21 10:18:46 end connection 127.0.0.1:5008