【问题标题】:How to update the existing fields, embedded mongodb documents in an array in Java如何更新现有字段,Java中数组中嵌入的mongodb文档
【发布时间】:2018-07-20 07:29:30
【问题描述】:

我有文档结构

{
    "Name" : "MongoDB",
    "Client" : "Database",
    "Details" : [ 
        {
            "Date" : "2018-07-18",
            "Code" : {
                "new" : "5",
                "new 1" : "3",
                "new 2" : "4"
            }
        }, 
        {
            "Date" : "2018-07-19",
            "Code" : {
                "new" : "7",
                "new 2" : "4"
            }
        }
  ]
}

我想将字段"new":"5" 更新为"Devops":"2,基于"Name":"Mongodb","Date":"2018-07-18"

【问题讨论】:

  • 到目前为止你尝试过什么?看到您的尝试将使人们更容易为您提供帮助。

标签: mongodb-java-3.3.0


【解决方案1】:

我在 Java 中尝试过,但是这段代码在同一日期附加了新文档

Bson filter =new Document("Name",name);

    Document h=new Document();
    Document dd=new Document();
    for(int i=0;i<ccode.length;i++)
      {
        h.append(ccode[i], hour[i]);
      }
     dd.append("Date", d);
     dd.append("Code", h);
    Document doc=new Document().append("Details",dd);
    System.out.println(dd);
    Bson updateOperationDocument = new Document("$push", doc);
    collection.updateOne(filter, updateOperationDocument);

【讨论】:

  • 我已经尝试过这个逻辑,但我遇到了同样的问题。
猜你喜欢
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 2017-06-05
相关资源
最近更新 更多