zoukankan      html  css  js  c++  java
  • 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

    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.

    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

    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

    Run the program to update mongodb document using java. Notice the salary is changed

    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.

  • 相关阅读:
    Python开发环境搭建(Windows)
    Pycharm安装第三方库
    Python 更新pip报错:Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问
    centos7.4 yum install报错:Another app is currently holding the yum lock; waiting for it to exit...
    [PySide]在简易在线词典中使用多线程
    三菱PLC——Dx Developer的使用
    关于BeautifulSoup编码的一些问题
    超简单的Ghost安装双系统方法
    《Visual C++ 2008入门经典》学习笔记(1)——数据类型
    win下硬盘安装ubuntu(无需光驱、U盘)
  • 原文地址:https://www.cnblogs.com/haoliansheng/p/6255543.html
Copyright © 2011-2022 走看看