Adding composite index with arrays in C#

127 views
Skip to first unread message

mn1986

unread,
Jul 8, 2011, 3:02:29 PM7/8/11
to mongodb-user
Hi all,
I am stuck at an issue with composite indexes. I have a case where my
JSON structure looks like:

"Person": {
"Name": "John",
"Surname": "Doe",
"Age": 20,
},
"Properties": [
{
"PropertyName": "company",
"PropertyValue": "abc"
},
{
"PropertyName": "address",
"PropertyValue": "xyz"
}
}

Now say I want to add index Index1 (Person.Name, Person.Surname,
Person.Age, Properties.PropertyName)

I do this as follows-

collection.DropAllIndexes();
string[] arr1 = new string[] { "Person.Name", "Person.Surname",
"Person.Age", "Properties.PropertyName"};
collection.EnsureIndex(IndexKeys.Ascending(arr1),
IndexOptions.SetName("Index1").SetUnique(true));

Now, even if I add another JSON with same Name, Surname, Age but
different properties it gives me SafeModeException as -

Safemode detected an error 'E11000 duplicate key error index: $Index1
dup key: { : "John", : "Doe", : 20 , : null }'.

I am not sure what the NULL stands for here. Also, if I create 2
indexes one for first 3 parameters Name, Surname, Age and another for
just PropertyName, then it gives the same error during verification
with second index with $propertynameindex dup key: { : null }'

Please help!!

Robert Stam

unread,
Jul 8, 2011, 3:42:28 PM7/8/11
to mongodb-user
This behavior doesn't seem to be specific to the C# driver. I can get
the same result using just the mongo shell:

> db.test.remove()
> db.test.ensureIndex(
... {"Person.Name" : 1, "Person.Surname" : 1, "Person.Age" : 1,
"Properties.ProperyName" : 1},
... { unique : true }
... )
> db.test.insert({
... Person : { Name : "John", Surname : "Doe", Age : 20 },
... Properties : [
... { PropertyName : "company", PropertyValue : "abc" },
... { PropertyName : "address", PropertyValue : "xyz" }
... ]})
> db.test.insert({
... Person : { Name : "John", Surname : "Doe", Age : 20 },
... Properties : [
... { PropertyName : "company", PropertyValue : "def" },
... { PropertyName : "address", PropertyValue : "ghi" }
... ]})
E11000 duplicate key error index: test.test.
$Person.Name_1_Person.Surname_1_Person.Age_1_Properties.ProperyName_1
dup k
ey: { : "John", : "Doe", : 20.0, : null }
>

It must have something to do with how arrays are indexed.
Reply all
Reply to author
Forward
0 new messages