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. flume监控

    Flume本身提供了http, ganglia的监控服务,而我们目前主要使用zabbix做监控.因此,我们为Flume添加了zabbix监控模块,和sa的监控服务无缝融合. 另一方面,净化Flume的 ...

  2. MySQL源代码解读

    第一步: 下载bison-2.4.1-setup.exe链接地址 第二步: 下载cmake-2.8.6-win32-x86.exe链接地址 第三步: 下载MySQL链接地址 G:\Mlearn\mys ...

  3. java 加密

    加密.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密. 双向加密大体意思就是明文加密后形成密文,能够通过算法还原成明文. 单向加密仅仅是对信息进行了摘要计算,不能通过算法生成明文. ...

  4. BackBone.js之Router

    一.前言 有一段时间没有写随笔了,可能是最近的烦心事有点多.不倾诉了,开始我们的主题吧,以前做过一个web的聊天平台,js的代码足足有2k行. 虽然是在一个星期就完成了,但是想想还是不服.一定有一种更 ...

  5. redis数据淘汰策略

    概述 在 redis 中,允许用户设置最大使用内存大小 server.maxmemory,在内存限定的情况下是很有用的.譬如,在一台 8G 机子上部署了 4 个 redis 服务点,每一个服务点分配 ...

  6. [pip]安装和管理python第三方包

    使用 ”pip install 包名“   直接下载安装第三方包 1.在以下地址下载最新的PIP安装文件:http://pypi.python.org/pypi/pip#downloads2.下载Wi ...

  7. python之模块colorsys颜色转换模块 暂不了解

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块colorsys #颜色转换模块 #colorsys import colorsys 针对 ...

  8. mongobooster 的使用

    mongobooster是mongodb的客户端工具 1.配置数据库 file->connect..->from URL 2.数据查询 选中数据库名,右击-Open Shell->输 ...

  9. 自己动手做——简易下载工具 SimpleDownloader

    一.说明 二.开发 三.源代码 源代码:https://github.com/FrankFan/SimpleDownloader 可执行文件下载地址

  10. asp:ObjectDataSource+asp:GridView 实现真分页

    <asp:GridView ID="GridViewCacheManager" DataSourceID="OdsCacheManager" runat= ...