【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!" }` | *** ** * ** ***

相关推荐
廿一夏5 小时前
MySql存储引擎与索引
数据库·sql·mysql
lzhdim7 小时前
SQL 入门 15:SQL 事务:从 ACID 到四种常见的并发问题
数据库·sql
瀚高PG实验室8 小时前
瀚高企业版V9.1.1在pg_restore还原备份文件时提示extract函数语法问题
数据库·瀚高数据库
TDengine (老段)8 小时前
TDengine Tag 设计哲学与 Schema 变更机制
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
YOU OU9 小时前
Spring IoC&DI
java·数据库·spring
Muscleheng10 小时前
Navicat连接postgresql时出现‘datlastsysoid does not exist‘报错
数据库·postgresql
罗超驿11 小时前
18.事务的隔离性和隔离级别:MySQL面试高频考点全解析
数据库·mysql·面试
jran-11 小时前
Redis 命令
数据库·redis·缓存
小江的记录本11 小时前
【Java基础】Java 8-21新特性:JDK21 LTS:虚拟线程、模式匹配switch、结构化并发、序列集合(附《思维导图》+《面试高频考点清单》)
java·数据库·python·mysql·spring·面试·maven
June`12 小时前
多线程redis下如何解决aof重写和rdb持久化的数据一致性问题
数据库·redis·缓存