update document in mongodb using java -摘自网络
update document in mongodb using java:
Mongodb driver provides functionality to update document in mongodb using java. Update is a process in which single or multiple documents can be updated based on certain criteria. Let us see what javadoc says about update
12 <a title="class in com.mongodb.client.result" href="http://api.mongodb.org/java/3.0/com/mongodb/client/result/UpdateResult.html">UpdateResult</a> updateOne(<a title="interface in org.bson.conversions" href="http://api.mongodb.org/java/3.0/org/bson/conversions/Bson.html">Bson</a> filter,<a title="interface in org.bson.conversions" href="http://api.mongodb.org/java/3.0/org/bson/conversions/Bson.html">Bson</a> update)Update a single document in the collection according to the specified arguments.
- Parameters:
filter
– a document describing the query filter, which may not be null.update
– a document describing the update, which may not be null. The update to apply must include only update operators.- Returns:
- the result of the update one operation
Let us try to understand with an example. To update document in mongodb using java, consider we have below document in collection.
1
2
3
4
5
6
|
{
"_id" : ObjectId("55daa2f7e60dd21204306b77"),
"name" : "Harish Taware",
"salary" : 40000,
"type" : "FT"
}
|
Now we want to update the salary to 80000. We have to provide
- A document which will identify above document. Let us say we want to update salary where name is “Harish Taware”
- A document which will specify the “$set” operation and the value which will itself be a Document.
Consider below code
1
2
3
4
|
Bson filter = new Document("name", "Harish Taware");
Bson newValue = new Document("salary", 90000);
Bson updateOperationDocument = new Document("$set", newValue);
collection.updateOne(filter, updateOperationDocument);
|
Here,
- filter variable stores the document with name Harish Taware
- newValue is the document which specifies salary needs to be updated to 90000
- updateOperationDocument specifies that a set operation is to be performed.
- collection.updateOne(filter,updateOperationDocument) actually does the job of updating document.
Here is complete code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.thejavageek.mongodb;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class MongodbFind {
public static void main(String[] args) {
MongoClient client = new MongoClient("localhost", 27017);
MongoDatabase database = client.getDatabase("employee_db");
MongoCollection<Document> collection = database
.getCollection("employees");
Bson filter = new Document("name", "Harish Taware");
Bson newValue = new Document("salary", 90000);
Bson updateOperationDocument = new Document("$set", newValue);
collection.updateOne(filter, updateOperationDocument);
client.close();
}
}
|
Run the program to update mongodb document using java. Notice the salary is changed
Java
1
2
3
4
5
6
|
{
"_id" : ObjectId("55daa2f7e60dd21204306b77"),
"name" : "Harish Taware",
"salary" : 90000,
"type" : "FT"
}
|
Just like updateOne, we have updateMany() method to update multiple documents at once. I hope the article helped understand how to update document in mongodb using java.
update document in mongodb using java -摘自网络的更多相关文章
- mongodb 新建用户 -摘自网络
随着版本的更新,对在使用mongodb的业务也进行了版本升级,但是在drop掉一个数据库时,问题来了,原来的用户随着删除库也被删除掉,但是再想通过原来的语法db.addUser()添加,一直报错,提示 ...
- MongoDB联合查询 -摘自网络
1.简单手工关联 首先将结果查询出来放到一个变量里面,然后再查询 u = db.user.findOne({author:"wangwenlong"}); for(var p = ...
- MongoDB for Java
开发环境 操作系统:Windows7 IDE: MyEclipse Database: MongoDB 开发依赖库 bson-3.0.1.jar mongodb-driver-3.0.1.jar mo ...
- Ubuntu14.04下Mongodb的Java API编程实例(手动项目或者maven项目)
不多说,直接上干货! 若大家,不会安装的话,则请移步,随便挑选一种. Ubuntu14.04下Mongodb(在线安装方式|apt-get)安装部署步骤(图文详解)(博主推荐) Ubuntu14.04 ...
- [MongoDB]MongoDB与JAVA结合使用CRUD
汇总: 1. [MongoDB]安装MongoDB2. [MongoDB]Mongo基本使用:3. [MongoDB]MongoDB的优缺点及与关系型数据库的比较4. [MongoDB]MongoDB ...
- 【MongoDB for Java】Java操作MongoDB
上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过 ...
- 基于mongodb的java之增删改查(CRUD)
1,下载驱动https://github.com/mongodb/mongo-java-driver/downloads,导入工程java中 2,建立测试代码 import java.net.Unkn ...
- [转]MongoDB for Java】Java操作MongoDB
原文地址: MongoDB for Java]Java操作MongoDB 开发环境: System:Windows IDE:eclipse.MyEclipse 8 Database:mongoDB 开 ...
- mongodb在java驱动包下的操作(转)
推荐几章很有用的文章 java操作参考文档 http://www.cnblogs.com/hoojo/archive/2011/06/02/2068665.html http://blog.csdn. ...
随机推荐
- DOM笔记(十):JavaScript正則表達式
一.RegExp ECMAScript通过RegExp类型类支持正則表達式,语法和Perl类似: var exp = /pattern/flags; patternb部分是不论什么简单的或复杂的正則表 ...
- Atlas系列一:Atlas功能特点FAQ
1:Atlas是否支持多字符集? 支持,可以在test.cnf中指定. #默认字符集,设置该项后客户端不再需要执行SET NAMES语句charset = utf8 2:Atlas是否支持事物操作? ...
- 排序基础之非比较的计数排序、桶排序、基数排序(Java实现)
转载请注明原文地址: http://www.cnblogs.com/ygj0930/p/6639353.html 比较和非比较排序 快速排序.归并排序.堆排序.冒泡排序等比较排序,每个数都必须和其他 ...
- Java并发容器——CopyOnWriteArrayList
CopyOnWriteArrayList是“读写分离”的容器,在写的时候是先将底层源数组复制到新数组中,然后在新数组中写,写完后更新源数组.而读只是在源数组上读.也就是,读和写是分离的.由于,写的时候 ...
- JavaWeb应用项目部署到云ubuntu
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6383068.html 在前面的博文中,我们已经在云主机ubuntu上搭建好jdk.tomcat以及mysql了 ...
- 【Linux】awk详细介绍
awk简介 awk是一种使用方便且表现力很强的编程语言,它可以应用在多种不同的计算与数据处理任务中.由于awk天生提供对文件中文本分列进行处理,所以如果一个文件中的每行都被特定的分隔符(常见的是空格) ...
- Reading CheckBoxes and Radio Buttons
Input tags with the type attribute checkbox can be grouped like radio buttons so that several checkb ...
- jQuery 绑定事件及移除绑定事件方法和元素事件列表
1.jQuery Event 事件: ready(fn); $(document).ready()注意在body中没有onload事件,否则该函数不能执行.在每个页面中可以有很多个函数被加载 ...
- HotSpot学习(一)——如何下载openjdk源码
下载页地址: https://download.java.net/openjdk/jdk7 或者 https://download.java.net/openjdk/jdk8 这里以jdk8的页面为例 ...
- adb forward交互流程
命令:adb forward tcp:6100 tcp:7100 // PC上所有6100端口通信数据将被重定向到手机端7100端口server上 或者adb forward tcp:6100 loc ...