遇到_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" }
>

mongodb - save()和insert()的区别的更多相关文章

  1. MongoDB的save 和insert函数的区别

    mongodb的save和insert函数都可以向collection里插入数据,但两者是有两个区别: 一.使用save函数里,如果原来的对象不存在,那他们都可以向collection里插入数据,如果 ...

  2. MongoDB save()方法和insert()方法的区别

    MongoDB save()方法和insert()方法的区别 首先看官方文档怎么说的 Updates an existing document or inserts a new document, d ...

  3. 关于python中使用mongodb模块,save和insert的小问题

    今天写python脚本的时候发现这样一个问题: import os , string , datetime ,pymongo; conn = pymongo.Connection("127. ...

  4. 关于thymeleaf th:replace th:include th:insert 的区别

    关于thymeleaf th:replace th:include th:insert 的区别    th:insert   :保留自己的主标签,保留th:fragment的主标签.    th:re ...

  5. Java MongoDB : Save image example

    In this tutorial, we show you how to save an image file into MongoDB, via GridFS API. The GridFS API ...

  6. mangodb之save与insert区别

    save:未指定 _id 参数 插入成功,自动生成_id指定 _id 但 _id 对应的记录不存在 插入成功,_id不变指定 _id 但 _id 对应的记录存在 根据_id,更新记录 insert: ...

  7. save(),saveOrUpdate(),merge()的区别

    Save save()方法能够保存实体到数据库,正如方法名称save这个单词所表明的意思.我们能够在事务之外调用这个方法,这也是我不喜欢使用这个方法保存数据的原因.假如两个实体之间有关系(例如empl ...

  8. Hibernate的save()和persist()的区别

    hibernate之所以提供与save()功能几乎完全类似的persist()方法,一方面是为了照顾JPA的用法习惯.另一方面,save()和 persist()方法还有一个区别:使用 save() ...

  9. Mongodb 语法,update,insert,delete,find

    ---恢复内容开始--- db.Users.update({OrganizationCode:"Global"},{$set:{OrganizationCode:"Fre ...

随机推荐

  1. bzoj 4428: [Nwerc2015]Debugging调试

    4428: [Nwerc2015]Debugging调试 Description Your fancy debugger will not help you in this matter. There ...

  2. codevs 1779 单词的划分

    1779 单词的划分 时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description Problem有一个很长的由小写字母组成字符串.为了便于对 ...

  3. [转]详解spring 每个jar的作用

    spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2. ...

  4. BZOJ 4031: [HEOI2015]小Z的房间 高斯消元 MartixTree定理 辗转相除法

    4031: [HEOI2015]小Z的房间 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个 ...

  5. 时间同步Servname not supported for ai_socktype

    rdate -s 129.7.1.66rdate: 129.7.1.66: Servname not supported for ai_socktype ntpdate 0.centos.pool.n ...

  6. winform listview默认第一项光标选中

    if (this.lsvSortingHeadList.Items.Count > 0) { this.lsvSortingHeadList.Focus(); this.lsvSortingHe ...

  7. Inno Setup入门(二十五)——Inno Setup类参考(11)

    http://379910987.blog.163.com/blog/static/3352379720126693742406/ 今天说说TNewCheckListBox类.该类和ListBox差不 ...

  8. 如何在windows下的Python开发工具IDLE里安装其他模块

    以安装Httplib2模块为例 1 下载模块 到 “https://code.google.com/p/httplib2/” 下载一款适合你的压缩包“httplib2-0.4.0.zip” 2 解压下 ...

  9. javascript 中contentWindow和 frames和iframe之间通信

    iframe父子兄弟之间通过jquery传值(contentWindow && parent),iframe的调用包括以下几个方面:(调用包含html dom,js全局变量,js方法) ...

  10. ubuntu下cmake自动化编译的一个例子

    一个CMakeLists.txt的例子参考:https://www.hahack.com/codes/cmake/https://blog.csdn.net/afei__/article/detail ...