database 如何停止在 mongodb 集合中插入重复文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24122981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:15:48  来源:igfitidea点击:

How to stop insertion of Duplicate documents in a mongodb collection

mongodbmongodb-querydatabasenosql

提问by shashank

Let us have a MongoDBcollection which has three docs..

让我们有一个MongoDB包含三个文档的集合..

db.collection.find()

db.collection.find()

 { _id:'...', user: 'A', title: 'Physics',   Bank: 'Bank_A' }
 { _id:'...', user: 'A', title: 'Chemistry', Bank: 'Bank_B' }
 { _id:'...', user: 'B', title: 'Chemistry', Bank: 'Bank_A' }

We have a doc,

我们有一个文档,

 doc = { user: 'B', title: 'Chemistry', Bank:'Bank_A' }

If we use

如果我们使用

 db.collection.insert(doc) 

here, this duplicate doc will get inserted in database.

在这里,这个重复的文档将被插入到数据库中。

 { _id:'...', user: 'A', title: 'Physics',   Bank: 'Bank_A' }
 { _id:'...', user: 'A', title: 'Chemistry', Bank: 'Bank_B' }
 { _id:'...', user: 'B', title: 'Chemistry', Bank: 'Bank_A' }
 { _id:'...', user: 'B', title: 'Chemistry', Bank: 'Bank_A' }

How this duplicate can be stopped. On which field should indexing be done or any other approach?

如何停止这种重复。应该在哪个字段上进行索引或任何其他方法?

回答by Vic

Don't use insert.

不要使用插入。

Use update with upsert=true. Update will look for the document that matches your query, then it will modify the fields you want and then, you can tell it upsert:True if you want to insert if no document matches your query.

更新与upsert=true. Update 将查找与您的查询匹配的文档,然后它会修改您想要的字段,然后,如果没有文档与您的查询匹配,您可以告诉它 upsert:True 如果要插入。

db.collection.update(
   <query>,
   <update>,
  {
    upsert: <boolean>,
     multi: <boolean>,
    writeConcern: <document>
   }
  )

So, for your example, you could use something like this:

因此,对于您的示例,您可以使用以下内容:

db.collection.update(doc, doc, {upsert:true})

回答by John Petrone

You should use a compound index on the set of fields that uniquely identify a document within your MongoDB collection. For example, if you decide that the combination of user, title and Bank are your unique key you would issue the following command:

您应该在唯一标识 MongoDB 集合中的文档的一组字段上使用复合索引。例如,如果您决定 user、title 和 Bank 的组合是您的唯一键,您将发出以下命令:

db.collection.createIndex( { user: 1, title: 1, Bank: 1 }, {unique:true} )

Please note that this should be done after you have removed previously stored duplicates.

请注意,这应该在您删除以前存储的重复项后完成。

http://docs.mongodb.org/manual/tutorial/create-a-compound-index/

http://docs.mongodb.org/manual/tutorial/create-a-compound-index/

http://docs.mongodb.org/manual/tutorial/create-a-unique-index/

http://docs.mongodb.org/manual/tutorial/create-a-unique-index/

回答by Creem

It has been updated from the above answers.

它已从上述答案更新。

please use db.collection.updateOne()instead of db.collection.update(). and also db.collection.createIndexes()instead of db.collection.ensureIndex()

请使用db.collection.updateOne()代替db.collection.update()。也db.collection.createIndexes()代替db.collection.ensureIndex()

Update: the methods update() and ensureIndex() has been deprecated from mongodb 2.*, you can see more details in mongoand the path is ./mongodb/lib/collection.js. For update(), the recommend methods are updateOne, updateMany, or bulkWrite. For ensureIndex(), the recommend method is createIndexes.

更新:方法 update() 和 ensureIndex() 已从 mongodb 2.* 中弃用,您可以在mongo 中查看更多详细信息,路径为./mongodb/lib/collection.js. 对于update(),推荐的方法是updateOne, updateMany, or bulkWrite。对于ensureIndex(),推荐方法是createIndexes