简体   繁体   English

使用Java将大量的Insert和Save组合到mongodb中? 有没有办法可以实现这一目标?

[英]Combining bulk Insert and Save in mongodb using Java? Is there a way through which we can achieve it?

I am pretty sure how INSERT works in mongodb, but I want insert to insert the data and in case the Id already exists I want to update the data like SAVE in mongodb. 我非常确定INSERT在mongodb中的工作方式,但是我想通过插入来插入数据,并且如果ID已经存在,我想在mongodb中更新数据,例如SAVE。

Example: 例:

If my entry in test collection is like, 如果我在测试集中的输入是

{ "_id":ObjectId("68768242ge"), "name":"1"  } 

I know using save am able to update the current data as 我知道使用保存能够将当前数据更新为

db.test.save({ "_id":ObjectId("68768242ge"), "name":"2"  }) 

But the list update is possible only using INSERT query. 但是只能使用INSERT查询来更新列表。

db.test.insert({ "_id":ObjectId("68768242ge"), "name":"2"  }) 

I will get an error in this case as a duplicate key. 在这种情况下,我将得到一个错误作为重复密钥。

So I want to do both the operation, If the object is not there, Then I want to insert it but where as if the Object key already exists then I want to update it. 因此,我想同时执行这两项操作,如果对象不存在,那么我想插入它,但是如果对象键已经存在,那么我想对其进行更新。

Is there any way through which we can achieve it? 有什么方法可以实现? I want to do the bulk insert / update using mongodb. 我想使用mongodb进行批量插入/更新。

What you want to do is called "upsert". 您要执行的操作称为“ upsert”。

Some updates also create records. 一些更新也会创建记录。 If an update operation specifies the upsert flag and there are no documents that match the query portion of the update operation, then MongoDB will convert the update into an insert. 如果更新操作指定了upsert标志,并且没有与更新操作的查询部分匹配的文档,则MongoDB将把更新转换为插入。

> db.test.update({"_id" : ObjectId("529f4ddd6487ccbe70e44c75")},{"name":"orig"},{upsert: true})
> db.test.find(); { "_id" : ObjectId("529f502aef047d0c12c305d5"), "name" : "orig" }
> db.test.update({"_id" : ObjectId("529f502aef047d0c12c305d5")},{"name":"new"},{upsert: true});
> db.test.find(); { "_id" : ObjectId("529f502aef047d0c12c305d5"), "name" : "new" }

You can check the MongoDB documentation of db.collection.update() . 您可以查看db.collection.update()的MongoDB文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以通过在 Java 中使用变长参数来实现动态数组吗? - Can we achieve a dynamic array by using variable length arguments in Java? 使用 Java 进行 BULK INSERT 的最佳方法是什么? - What is the best way to do BULK INSERT using Java? Java MongoDB 从流中批量插入 - Java MongoDB bulk insert from stream 有没有办法使用 Hibernate 批量插入或更新记录 - Is there a way for bulk insert or update of records using Hibernate 使用Java和Spring数据在mongodb中批量插入仅插入一个文档 - bulk insert in mongodb with Java and Spring data inserts only one document 有什么方法可以使用java将数组数据存储在变量中并进一步使用该变量 - Is there any way through which i can store array data in variable using java and use that variable further 我们可以使用mongodb和Java进行数据表服务器端分页吗? - Can we do datatables server side pagination using mongodb and java? 我们可以在Java中使用_id更新mongodb中的文档吗? - can we update a document in mongodb using _id in java? 我们可以使用Java中的机器人类插入Unicode字符吗? - Can we insert Unicode Characters using Robot Class in Java? 我们如何使用 java clickhouse-client 批量插入? - how can we Batch insert using java clickhouse-client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM