MongoDB Java驱动程序更新子文档

MongoDB Java驱动程序更新子文档,java,mongodb,Java,Mongodb,我正试图通过MongoDB Java驱动程序更新一个子文档 我在Mongo有以下结构: { "_id" : "9999996978c9df5b02999999", "title" : "The Effects of Glaze on Pottery", "numberOfDownloads" : "453", "summary" : "This is a summary of the document", "

我正试图通过MongoDB Java驱动程序更新一个子文档

我在Mongo有以下结构:

    {
        "_id" : "9999996978c9df5b02999999",
        "title" : "The Effects of Glaze on Pottery",
        "numberOfDownloads" : "453",
        "summary" : "This is a summary of the document",
        "documents" : {
                "file" : "99991F64C16E9A0954E89999",
                "mimetype" : "document/pdf",
                "pages" : "415",
                "downloads" : "453"
        }
}
使用Java驱动程序,我可以通过以下方式更新根文档“numberOfDownloads”字段:

        Document updDocQuery = new Document("_id", "9999996978c9df5b02999999");

        Document updDocValues = new Document("numberOfDownloads","555");

        Document updDocSet = new Document("$set",updDocValues);

        System.out.println(updDocSet.toJson());

        collection.updateOne(updDocQuery, updDocSet);
Document upDocValue = new Document("numberOfDownloads": "453")
                      .append("documents.downloads":"453");
这很好用

现在,我还尝试更新子文档“documents”,以将“downloads”值设置为相同的值,并且我正在尝试以下操作:

        Document updDocQuery = new Document("_id", "9999996978c9df5b02999999");

        Document updDocValues = new Document("numberOfDownloads","555");

        Document updSubDocValues = new Document("downloads","555");

        updDocValues.append("documents", updSubDocValues);
        Document updDocSet = new Document("$set",updDocValues);

        System.out.println(updDocSet.toJson());

        collection.updateOne(updDocQuery, updDocSet);
这会将“documents”属性及时更新为我的新值,但我希望更新特定字段,并保持其他字段不变

我最终在Mongo中得到了一份以下文件:

    {
        "_id" : "9999996978c9df5b02999999",
        "title" : "The Effects of Glaze on Pottery",
        "numberOfDownloads" : "555",
        "summary" : "This is a summary of the document",
        "documents" : {
                "downloads" : "555"
        }
}
我需要的是:

    {
        "_id" : "9999996978c9df5b02999999",
        "title" : "The Effects of Glaze on Pottery",
        "numberOfDownloads" : "555",
        "summary" : "This is a summary of the document",
        "documents" : {
                "file" : "99991F64C16E9A0954E89999",
                "mimetype" : "document/pdf",
                "pages" : "415",
                "downloads" : "555"
        }
}
我更希望不必拉取记录,将其转换为对象,更新值并提交更新,但这似乎效率低下

我可以从Mongo控制台执行此查询,效果非常好:

db.Paper.update(
    {"_id" : "5617776978c9df5b02f68228"},
    {$set: 
        { "numberOfDownloads" : "453", 
          "documents" : 
                { "downloads" : "453"}
        }
    });

我只是错过了一些简单的事情,还是我把事情复杂化了

如果这是mongodb中的更新集:

 {$set: 
        { "numberOfDownloads" : "453", 
          "documents" : 
                { "downloads" : "453"}
        }
 }
可以通过以下方式使用Document类:

        Document updDocQuery = new Document("_id", "9999996978c9df5b02999999");

        Document updDocValues = new Document("numberOfDownloads","555");

        Document updDocSet = new Document("$set",updDocValues);

        System.out.println(updDocSet.toJson());

        collection.updateOne(updDocQuery, updDocSet);
Document upDocValue = new Document("numberOfDownloads": "453")
                      .append("documents.downloads":"453");
这将为您提供:

{
  "numberOfDownloads": "453",
  "documents" : 
    { "downloads" : "453"}
}
{$set: 
      { "numberOfDownloads" : "453", 
            "documents" : 
                  { "downloads" : "453"}
      }
}
然后,您可以使用以下内容创建外部文档:

Document upDocSet = new Document("$set",updDocValue);
这应该给你:

{
  "numberOfDownloads": "453",
  "documents" : 
    { "downloads" : "453"}
}
{$set: 
      { "numberOfDownloads" : "453", 
            "documents" : 
                  { "downloads" : "453"}
      }
}
然后在此处运行查询:

collection.updateOne(upDocQuery,upDocSet);
所以你最终会:

Document updDocQuery = new Document("_id", "9999996978c9df5b02999999");

Document upDocValue = new Document("numberOfDownloads": "453")
                          .append("documents.downloads":"453");

Document upDocSet = new Document("$set",updDocValue);

collection.updateOne(upDocQuery,upDocSet);

错误是什么?它正在覆盖
文档
对象?正确。与最终得到具有更新值的子对象不同,整个子文档将被覆盖,因此它只有downloads属性。我用关于当前输出和所需输出的更多信息更新了原始帖子。我在文档中看到了这一点-我试图在一个updateOne()中完成这两个更新,而不是多次更新。如果可能的话,你知道吗?例如:更新根“numberOfDownloads”属性和子文档的“downloads”属性。如果我必须分两步进行,我可以接受,只需要寻找一个类似于我可以在Mongo中本机执行的查询的单步更新。再次感谢!工作起来很有魅力——我现在看到了嵌入文档和您的版本只是附加了第二个标准的区别。谢谢