Luís Lobo
2014-02-12 11:10:32 UTC
Hi!
I've been having trouble with (I thought random) duplicate key errors in
mongo.And came to a conclusion that I hope helps others.
Imagine a 'foo' collection with objects like this one:
*{ "key": { "group_id": 1, "date_day": ISODate("2014-02-12T00:00:00Z")
}, counter: 0 }**
*
The collection has a unique index:
*> db.foo.ensureIndex({'key.group_id': 1, 'key.date_day': 1}, {unique:
true})*
If I perform:
*db.foo.update({"key": {"group_id": 1, "date_day": new
Date('2014-02-12')} }, {'$inc': {'counter': 1}}, true /* , true);**
*
I have no problemas and the outcome (as expected) is:
*> db.foo.find()**
**{ "_id" : ObjectId("52fb50302597bd0cde688216"), "key" : { "group_id" :
1, "date_day" : ISODate("2014-02-12T00:00:00Z") }, "counter" : 1 }**
*
But if I change the order of the fields in the key I get:
*> db.foo.update({"key": {"date_day": new Date('2014-02-12'),
"group_id": 1} }, {'$inc': {'counter': 1}}, true, true);**
**E11000 duplicate key error index: test.foo.$group_id_1_date_day_1 dup
key: { : 1.0, : new Date(1392163200000) }**
*
This makes some sense if you test this:
*> var k1 = {"date_day": new Date('2014-02-12'), "group_id": 1};**
**> var k2 = {"group_id": 1, "date_day": new Date('2014-02-12')};**
**> print(k1 == k2);**
**false**
*
I guess you need to be very careful with the order of fields in mongo.
The real strange part is that, if you perform bulk updates in the shell, something like:
*db.foo.update({}, {"$set": {"key.date_day": new Date()}})**
*
You _*may*_ end up with *'key'* being reordered - I couldn't reproduce it, but it happened to me in the past.
Awaitting your wise comments :)
Regards,
--
Luís Filipe Lobo <lobo-***@public.gmane.org>
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
I've been having trouble with (I thought random) duplicate key errors in
mongo.And came to a conclusion that I hope helps others.
Imagine a 'foo' collection with objects like this one:
*{ "key": { "group_id": 1, "date_day": ISODate("2014-02-12T00:00:00Z")
}, counter: 0 }**
*
The collection has a unique index:
*> db.foo.ensureIndex({'key.group_id': 1, 'key.date_day': 1}, {unique:
true})*
If I perform:
*db.foo.update({"key": {"group_id": 1, "date_day": new
Date('2014-02-12')} }, {'$inc': {'counter': 1}}, true /* , true);**
*
I have no problemas and the outcome (as expected) is:
*> db.foo.find()**
**{ "_id" : ObjectId("52fb50302597bd0cde688216"), "key" : { "group_id" :
1, "date_day" : ISODate("2014-02-12T00:00:00Z") }, "counter" : 1 }**
*
But if I change the order of the fields in the key I get:
*> db.foo.update({"key": {"date_day": new Date('2014-02-12'),
"group_id": 1} }, {'$inc': {'counter': 1}}, true, true);**
**E11000 duplicate key error index: test.foo.$group_id_1_date_day_1 dup
key: { : 1.0, : new Date(1392163200000) }**
*
This makes some sense if you test this:
*> var k1 = {"date_day": new Date('2014-02-12'), "group_id": 1};**
**> var k2 = {"group_id": 1, "date_day": new Date('2014-02-12')};**
**> print(k1 == k2);**
**false**
*
I guess you need to be very careful with the order of fields in mongo.
The real strange part is that, if you perform bulk updates in the shell, something like:
*db.foo.update({}, {"$set": {"key.date_day": new Date()}})**
*
You _*may*_ end up with *'key'* being reordered - I couldn't reproduce it, but it happened to me in the past.
Awaitting your wise comments :)
Regards,
--
Luís Filipe Lobo <lobo-***@public.gmane.org>
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.