MongoDB小结07 - update【$addToSet & $each】

用$addToSet更新可以避免重复,将它与$each组合起来,可以一次性添加多条(就算后添加的值已存在也没有关系)
db.user.update({"name":"codingwhy.com"},{"$addToSet":{"hobby":{"$each":["singing","eating","dancing"]}}})
{ "_id" : ObjectId("55082691591555a6c35dd698"), "name" : "codingwhy.com", "email" : "[email protected]", "hobby" : [ "swimming", "running", "reading", "sleeping", "singing", "eating", "dancing" ] }

你可能感兴趣的:(mongodb)