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

相关推荐
老华带你飞21 分钟前
实习记录小程序|基于SSM+Vue的实习记录小程序设计与实现(源码+数据库+文档)
java·数据库·spring boot·小程序·论文·毕设·实习记录小程序
Elastic 中国社区官方博客1 小时前
Elasticsearch 索引副本数
大数据·数据库·elasticsearch·搜索引擎·全文检索
冬瓜的编程笔记2 小时前
【八股战神篇】MySQL高频面试题
数据库·mysql·面试
赵渝强老师2 小时前
【赵渝强老师】Memcached的路由算法
数据库·redis·nosql·memcached
belldeep2 小时前
groovy 如何遍历 postgresql 所有的用户表 ?
数据库·postgresql
2401_896008192 小时前
PostgreSQL
数据库·postgresql
搞不懂语言的程序员2 小时前
Redis Sentinel如何实现高可用?
数据库·redis·sentinel
wangzhongyudie3 小时前
SQL实战:06交叉日期打折问题求解
数据库·sql
Liudef064 小时前
使用Docker部署MongoDB
mongodb·docker·容器