【sql】MongoDB 新增 高级用法

【sql】MongoDB 新增 高级用法




批量插入数据, 遇到错误跳过并继续执行


方案一

ordered:false

使用场景:

1: 数据存在时则跳过插入

//批量插入

db.res_phone.insertMany(

{"_id":1, "phone":10086}, {"_id":2, "phone":10010}, {"_id":3, "phone":10000}, \], { //遇到错误是否中断 false遇到错误会跳过 继续执行 ordered:false } ); *** ** * ** *** 方案二 update里有个参数 '$setOnInsert' 可以实现"存在则不执行"的功能,可见 $setOnInsert 官方文档。 **示例:** 起始数据: |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `> db.Blog.``insert``({``"_id"``:``"123456"``, ``"blog_cont"``:``"abcdef"``, ``"title"``:``"《My Test》"``})` `> db.Blog.find()` `{ ``"_id"` `: ``"123456"``, ``"blog_cont"` `: ``"abcdef"``, ``"title"` `: ``"《My Test》"` `}` | 加入相同 ID 的日志: |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `> db.Blog.``update``({``"_id"``:``"123456"``}, {$setOnInsert:{``"blog_cont"``:``"abc123"``, ``"other"``:``"hello world!"``}}, {upsert:``true``})` `WriteResult({ ``"nMatched"` `: 1, ``"nUpserted"` `: 0, ``"nModified"` `: 0 })` `>` `> db.Blog.find()` `{ ``"_id"` `: ``"123456"``, ``"blog_cont"` `: ``"abcdef"``, ``"title"` `: ``"《My Test》"` `}` | 加入不同 ID 的日志: |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `> db.Blog.``update``({``"_id"``:``"123"``}, {$setOnInsert:{``"blog_cont"``:``"abc123"``, ``"other"``:``"hello world!"``}}, {upsert:``true``})` `WriteResult({ ``"nMatched"` `: 0, ``"nUpserted"` `: 1, ``"nModified"` `: 0, ``"_id"` `: ``"123"` `})` `>` `> db.Blog.find()` `{ ``"_id"` `: ``"123456"``, ``"blog_cont"` `: ``"abcdef"``, ``"title"` `: ``"《My Test》"` `{ ``"_id"` `: ``"123"``, ``"blog_cont"` `: ``"abc123"``, ``"other"` `: ``"hello world!"` `}` | 如果某些内容想更新的话(例如更新 title )可以用 '$set': |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `> db.Blog.``update``({``"_id"``:``"123456"``}, {$setOnInsert:{``"blog_cont"``:``"abc123"``, ``"other"``:``"hello world!"``}, $``set``:{``"title"``:``"《New Title》"``}}, {upsert:``true``})` `WriteResult({ ``"nMatched"` `: 1, ``"nUpserted"` `: 0, ``"nModified"` `: 1 })` `>` `> db.Blog.find()` `{ ``"_id"` `: ``"123456"``, ``"blog_cont"` `: ``"abcdef"``, ``"title"` `: ``"《New Title》` `{ "``_id``" : "``123``", "``blog_cont``" : "``abc123``", "``other``" : "``hello world!" }` | *** ** * ** ***

相关推荐
哆啦A梦15881 小时前
Springboot整合MyBatis实现数据库操作
数据库·spring boot·mybatis
Zzzzmo_1 小时前
【MySQL】JDBC(含settings.xml文件配置/配置国内镜像以及pom.xml文件修改)
数据库·mysql
FirstFrost --sy2 小时前
MySQL内置函数
数据库·mysql
2401_879693872 小时前
将Python Web应用部署到服务器(Docker + Nginx)
jvm·数据库·python
reembarkation3 小时前
光标在a-select,鼠标已经移出,下拉框跟随页面滚动
java·数据库·sql
eggwyw3 小时前
MySQL-练习-数据汇总-CASE WHEN
数据库·mysql
星轨zb3 小时前
通过实际demo掌握SpringSecurity+MP中的基本框架搭建
数据库·spring boot·spring security·mp
treacle田3 小时前
达梦数据库-配置本地守护进程dmwatcher服务-记录总结
数据库·达梦数据库·达梦数据库local数据守护
wyt5314293 小时前
Redis的安装教程(Windows+Linux)【超详细】
linux·数据库·redis
CeshirenTester4 小时前
从数据库到结构化用例:一套可落地的测试智能体架构
数据库·架构