karloku
2015-05-19 18:28:04 +08:00
> db.testbed.update({"_id":ObjectId("555b0d58806a1600d242c861")}, {$set: {"name" : []}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.testbed.find(ObjectId("555b0d58806a1600d242c861"));
{ "_id" : ObjectId("555b0d58806a1600d242c861"), "name" : [ ] }
> db.testbed.update({"_id": ObjectId("555b0d58806a1600d242c861")}, {$addToSet: {"name" : {$each : [ "他", "他", "他", 1, 2, 3, "me", "me"]}}}, true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.testbed.find(ObjectId("555b0d58806a1600d242c861"));
{ "_id" : ObjectId("555b0d58806a1600d242c861"), "name" : [ "他", 1, 2, 3, "me" ] }
> db.testbed.update({"_id": ObjectId("555b0d58806a1600d242c861")}, {$addToSet: {"name2" : {$each : [ "他", "他", "他", 1, 2, 3, "me", "me"]}}}, true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.testbed.find(ObjectId("555b0d58806a1600d242c861"));
{ "_id" : ObjectId("555b0d58806a1600d242c861"), "name" : [ "他", 1, 2, 3, "me" ], "name2" : [ "他", 1, 2, 3, "me" ] }
tested mongodb-3.0.3
可能是以前版本的bug. 印象里以前还有过$addToSet w/ $each到一个事先不存在的field时会出现重复.