如何使用Java更新数组中的现有字段,嵌入式mongodb文档

时间:2018-07-20 07:29:30

标签: mongodb-java-3.3.0

我有文件结构

"compilerOptions": {
  "paths": { 
    "@angular/*": ["../../node_modules/@angular/*"],
    "rxjs/*": ["../../node_modules/rxjs/*"]
  },
}

我想基于{ "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

1 个答案:

答案 0 :(得分:0)

我用Java尝试过,但是这段代码将新文档附加到同一日期

Bson过滤器= 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);