Java的MongoDB:更新文档

While updating data in document present in any collection of our database, we will come across various scenarios, let us analyze them step by step.

在更新数据库任何集合中存在的文档中的数据时,我们将遇到各种情况,让我们逐步分析它们。

更新单个字段值 (Updating a single field value)

We will update the field name and change its value to ABC. To do so, we will have to first find the correct document to update the value, which we can do using some known field. In our case we will find the document with _id value xyz123 which will act as condition in our query and then we will update the value of the name field.

我们将更新字段name并将其值更改为ABC 。 为此,我们必须首先找到正确的文档来更新该值,我们可以使用一些已知字段来进行此操作。 在本例中,我们将找到具有_idxyz123的文档,该文档将作为查询中的条件,然后我们将更新name字段的值。

package com.mongo;

import com.mongodb.DB; 
import com.mongodb.MongoClient; 
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;

public class MakeConnection { 
	public static void main(String[] args) {
		try { 
			// code to create the connection
			MongoClient mongoClient = new MongoClient("localhost", 27017); 
			// code to connect to the database
			DB db = mongoClient.getDB("DemoDB");

			// get the User collection from DB
			DBCollection userCollection = db.getCollection("User");

			// condition query to get document which has to be updated
			BasicDBObject queryObj = new BasicDBObject();
			queryObj.put("_id", "xyz123");

			// create new document with data to be updated
			BasicDBObject newObj = new BasicDBObject();
			newObj.put("name", "ABC");

			// updating data into collection
			userCollection.update(queryObj, newObj);
		} 
		catch(Exception e) { 
			e.printStackTrace(); 
		} 
	}
}

In the above case the existing document will be completely replace, which means that if it had some other fields other than the name field, then those fields will get removed. But we don't want that, right. We want to be able to just update a single value in any document, in that case we should use update modifier $set.

在上述情况下,现有文档将被完全替换,这意味着如果name字段以外还有其他字段,则这些字段将被删除。 但是我们不想要那样,对。 我们希望能够只更新任何文档中的单个值,在这种情况下,我们应该使用update修饰符$set

The $set update modifier is a keyword which informs mongo that only the provided field should be updated in a document

$set update修饰符是一个关键字,用于通知mongo仅应在文档中更新提供的字段

Let's update the above code:

让我们更新上面的代码:

package com.mongo;

import com.mongodb.DB; 
import com.mongodb.MongoClient; 
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;

public class MakeConnection { 
	public static void main(String[] args) {
		try { 
			// code to create the connection
			MongoClient mongoClient = new MongoClient("localhost", 27017); 
			// code to connect to the database
			DB db = mongoClient.getDB("DemoDB");

			// get the User collection from DB
			DBCollection userCollection = db.getCollection("User");

			// condition query to get document which has to be updated
			BasicDBObject queryObj = new BasicDBObject();
			queryObj.put("_id", "xyz123");

			// create new document with data to be updated
			BasicDBObject newObj = new BasicDBObject();
			newObj.put("$set", new BasicDBObject.append("name", "ABC"));

			// updating data into collection
			userCollection.update(queryObj, newObj);
		} 
		catch(Exception e) { 
			e.printStackTrace(); 
		} 
	}
}

The code above presents the correct way for updating any document. The output of the above program will be:

上面的代码提供了更新任何文档的正确方法。 上面程序的输出将是:

Data Updation in MongoDB

The value of field name is updated from xyz to ABC.

字段名称的值从xyz更新为ABC

In the above case the existing document is

在上述情况下,现有文档为

更新文档字段中的数组 (Updating an Array in a Document field)

Considering a scenario, where we have an array for field friends

考虑一个场景,我们有一个供外地朋友使用的数组

Data Updation in MongoDB

and if we want to update that array by adding values, we can do it by referring the below code snippet:

如果我们想通过添加值来更新该数组,可以通过参考以下代码片段来实现:

Data Updation in MongoDB

and Amit is added to the array against field friends.

和阿米特(Amit)被加入阵营对抗野外朋友

Data Updation in MongoDB

更新“文档”字段中的对象 (Updating an Object in a Document field)

We can even add object to the array of object using update query. For an example, for against field comments, if we want to add multiple objects with various field, it is possible.

我们甚至可以使用更新查询将对象添加到对象数组。 例如,对于反对字段注释 ,如果我们要添加具有不同字段的多个对象,则是可能的。

Here is the code snippet:

这是代码片段:

Data Updation in MongoDB

and the corresponding output is:

相应的输出为:

Data Updation in MongoDB

翻译自: https://www.studytonight.com/mongodb/data-updation-mongodb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值