gpt4 book ai didi

java - MongoDB Java驱动程序更新子文档

转载 作者:行者123 更新时间:2023-11-30 03:09:52 25 4
gpt4 key购买 nike

我正在尝试通过 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 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);

这将“文档”属性及时更新为我的新值 - 但我想更新特定字段并保持其他字段不变。

我最终在 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 Console 执行此查询,效果很好:

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

我只是错过了一些简单的东西 - 或者 - 我是否过于复杂化了?

最佳答案

如果这是 mongodb 中设置的更新:

 {$set: 
{ "numberOfDownloads" : "453",
"documents" :
{ "downloads" : "453"}
}
}

您可以这样使用 Document 类:

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

这会给你:

{
"numberOfDownloads": "453",
"documents" :
{ "downloads" : "453"}
}

然后您可以使用以下命令创建外部文档:

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

这应该给你:

{$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);

关于java - MongoDB Java驱动程序更新子文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com