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

相关推荐
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger1 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug2 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom2 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试