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

 
1
2
<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 -摘自网络的更多相关文章

  1. mongodb 新建用户 -摘自网络

    随着版本的更新,对在使用mongodb的业务也进行了版本升级,但是在drop掉一个数据库时,问题来了,原来的用户随着删除库也被删除掉,但是再想通过原来的语法db.addUser()添加,一直报错,提示 ...

  2. MongoDB联合查询 -摘自网络

    1.简单手工关联 首先将结果查询出来放到一个变量里面,然后再查询 u = db.user.findOne({author:"wangwenlong"}); for(var p = ...

  3. MongoDB for Java

    开发环境 操作系统:Windows7 IDE: MyEclipse Database: MongoDB 开发依赖库 bson-3.0.1.jar mongodb-driver-3.0.1.jar mo ...

  4. Ubuntu14.04下Mongodb的Java API编程实例(手动项目或者maven项目)

    不多说,直接上干货! 若大家,不会安装的话,则请移步,随便挑选一种. Ubuntu14.04下Mongodb(在线安装方式|apt-get)安装部署步骤(图文详解)(博主推荐) Ubuntu14.04 ...

  5. [MongoDB]MongoDB与JAVA结合使用CRUD

    汇总: 1. [MongoDB]安装MongoDB2. [MongoDB]Mongo基本使用:3. [MongoDB]MongoDB的优缺点及与关系型数据库的比较4. [MongoDB]MongoDB ...

  6. 【MongoDB for Java】Java操作MongoDB

    上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过 ...

  7. 基于mongodb的java之增删改查(CRUD)

    1,下载驱动https://github.com/mongodb/mongo-java-driver/downloads,导入工程java中 2,建立测试代码 import java.net.Unkn ...

  8. [转]MongoDB for Java】Java操作MongoDB

    原文地址: MongoDB for Java]Java操作MongoDB 开发环境: System:Windows IDE:eclipse.MyEclipse 8 Database:mongoDB 开 ...

  9. mongodb在java驱动包下的操作(转)

    推荐几章很有用的文章 java操作参考文档 http://www.cnblogs.com/hoojo/archive/2011/06/02/2068665.html http://blog.csdn. ...

随机推荐

  1. mysql主从备份功能配置与測试

    在高訪问量服务环境下,单机配置mysql服务将无法满足频繁快速的数据读写操作. 一旦mysql出现故障造成数据丢失.无法恢复. 因此.在mysql服务上启用主从备份功能,支持读写分离技术.最靠可的是搭 ...

  2. Aerospike系列:2:商业版和社区版的比较

    http://www.aerospike.com/products-services

  3. MongoDB内存管理机制

    目前,MongoDB使用的是内存映射存储引擎,它会把磁盘IO操作转换成内存操作,如果是读操作,内存中的数据起到缓存的作用,如果是写操作,内存还可以把随机的写操作转换成顺序的写操作,总之可以大幅度提升性 ...

  4. vue框架整体流程

    1.整体流程 (1)模板解析成render函数 (2)响应式监听 (3)首次渲染,显示页面,绑定依赖 (4)data属性变化,触发rerender 2.模板解析为render函数 参考上一篇博客. 模 ...

  5. JavaWeb项目异常管理之log4j的使用教程

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6399191.html 在项目中的应用见: https://github.com/ygj0930/CoupleS ...

  6. dubbo注册中心介绍

    作者:微子Lee链接:https://www.jianshu.com/p/2f4cfb6ed048 Dubbo的注册中心有好多种,包括Multicast.Zookeeper.Redis.Simple等 ...

  7. docker容器日志在哪?以及清理命令

    /var/lib/docker/containers 日志大小限制:传送门 查看所有容器日志大小和清理所有容器日志命令: ls -lh $(find /var/lib/docker/container ...

  8. 程序员必备!Sonar代码质量管理工具

    Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具. Sonar 概述 Sonar 是一个用于代码质量管理的开放平台.通过插 ...

  9. centos中执行apt-get命令提示apt-get command not found

    先说结论: 在centos下用yum install xxx yum和apt-get的区别: 一般来说著名的linux系统基本上分两大类:  RedHat系列:Redhat.Centos.Fedora ...

  10. 大道至简的C语言内存管理

    C语言内存的开辟和释放需要程序员自己来实现. 而C语言的内存开辟和释放只在stdlib.h里面提供了四个函数,这么简洁的设计就足以完成一切工作. C++里面各种类型的指针漫天飞舞,显得啰嗦冗余. ca ...