mongodb - save()和insert()的区别
2016-03-15 19:25 abce 阅读(1095) 评论(0) 编辑 收藏 举报遇到_id相同的情况下:
insert操作会报错;save完成保存操作
> db.person.find() > db.person.insert({"_id":1,age:1,name:"hh"}) WriteResult({ "nInserted" : 1 }) > db.person.find() { "_id" : 1, "age" : 1, "name" : "hh" } #insert一个"_id"已经存在的文档 > db.person.insert({"_id":1,age:10,name:"hh02"}) WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error collection: foobar.person index: _id_ dup key: { : 1.0 }" } }) > db.person.find() { "_id" : 1, "age" : 1, "name" : "hh" } #save一个"_id"已经存在的文档 > db.person.save({"_id":1,age:10,name:"hh02"}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.person.find() { "_id" : 1, "age" : 10, "name" : "hh02" } >