场景描述:
现有类似 {key:"value",key1:"value1"}
这样的文档。
我使用db.collection.insertMany()
将文档批量插入到集合之中,例如:
<code>db.collection.insertMany([ {key:"1",key1:"value1"}, {key:"2",key1:"value1"}, {key:"3",key1:"value1"}, ……]);</code>
具体问题描述:
我需要key
的值是唯一的,在批量插入的时候自动舍弃掉有重复值的文档。
我有尝试使用db.collection.createIndex({key:1},{unique:true})
给这个集合添加 unique 索引,但结果是插入时如果遇到重复值,直接会中断插入并且抛出错误。
问题:
请问在数据库层面有没有解决此问题的办法?
【修改:】请问在数据库层面不将唯一值赋给“_id”有没有解决此问题的办法?
难道只有在语言层面做判断?
附:
db.collection.insertMany文档
unique index 文档
回复内容:
场景描述:
现有类似 {key:"value",key1:"value1"}
这样的文档。
我使用db.collection.insertMany()
将文档批量插入到集合之中,例如:
<code>db.collection.insertMany([ {key:"1",key1:"value1"}, {key:"2",key1:"value1"}, {key:"3",key1:"value1"}, ……]);</code>
具体问题描述:
我需要key
的值是唯一的,在批量插入的时候自动舍弃掉有重复值的文档。
我有尝试使用db.collection.createIndex({key:1},{unique:true})
给这个集合添加 unique 索引,但结果是插入时如果遇到重复值,直接会中断插入并且抛出错误。
问题:
请问在数据库层面有没有解决此问题的办法?
【修改:】请问在数据库层面不将唯一值赋给“_id”有没有解决此问题的办法?
难道只有在语言层面做判断?
附:
db.collection.insertMany文档
unique index 文档
找到目前比较好的办法:
-
设置
key
为unique
index。 -
当使用
db.collection.insertMany()
插入多文档时,使用ordered: false
选项跳过插入错误的文档,不中断插入操作。 -
最后对插入重复值抛出的异常做处理。
例如:
此处用
PHP
作为示例。
<code class="php">try { //首先对 key 字段添加唯一索引 $this->collection->createIndex(['key' => 1], ["unique" => true]);} catch (\MongoDB\Driver\Exception\Exception $e) { Log::error($e->getMessage());}try { $result = $this-<span style="color:transparent">本文来源gaodai#ma#com搞*!代#%^码$网!</span><strong>搞代gaodaima码</strong>>collection->insertMany( $data, ['ordered' => false] //跳过插入错误 );} catch (\MongoDB\Driver\Exception\BulkWriteException $e) { /*如果有重复数据插入,将抛出 BulkWriteException */ $result = $e->getWriteResult(); $writeErrors = $result->getWriteErrors(); $errorsAmount = count($writeErrors); //插入错误的数量 Log::info($errorsAmount . '条重复数据未插入!');} catch (\MongoDB\Driver\Exception\Exception $e) { Log::error($e->getMessage()); exit;}</code>
将key的值给_id
你看下你自己附带的文档,里面专门有提到的:
With ordered to false, the insert operation would continue with any remaining documents.
Unordered Inserts
The following attempts to insert multiple documents with _id field and ordered: false. The array of documents contains two documents with duplicate _id fields.
try {
db.products.insertMany( [
<code> { _id: 10, item: "large box", qty: 20 }, { _id: 11, item: "small box", qty: 55 }, { _id: 11, item: "medium box", qty: 30 }, { _id: 12, item: "envelope", qty: 100}, { _id: 13, item: "stamps", qty: 125 }, { _id: 13, item: "tape", qty: 20}, { _id: 14, item: "bubble wrap", qty: 30}</code>
], { ordered: false } );
} catch (e) {
print (e);
}