数据源中有同一个数据重复的情况,插入数据使用
dbObj.collection(tableName).insertOne(dataObj,function(err,res){});
会导致数据库中有重复的数据。
解决办法:
insertOne替换成updateOne:
dbObj.collection(tableName).updateOne(dataObj,{'$set':dataObj},{'upsert':true},function(err,res){});
根据官方文档的说明:
db.collection.updateOne(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string> // Available starting in MongoDB 4.2.1
}
)
find()
upsert:Optional. When true, updateOne() either:
* Creates a new document if no documents match the filter. For more details see upsert behavior.
* Updates a single document that matches the filter.
To avoid multiple upserts, ensure that the filter fields are uniquely indexed.
Defaults to false.
使用这个API,当upsert为true时,会根据条件filter去查数据库。
如果不存在,直接将update保存;
如果存在,将更新数据。
关于参数upsert和filter,官方文档中有特意指出
If upsert: true
and no documents match the filter
, db.collection.updateOne() creates a new document based on the filter
criteria and update
modifications. See Update with Upsert.
If you specify upsert: true
on a sharded collection, you must include the full shard key in the filter. For additional db.collection.updateOne() behavior on a sharded collection, see Sharded Collections.
参考:
https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#db.collection.updateOne