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