MongoDB Java驱动程序更新子文档

时间:2015-11-20 18:34:21

标签: java mongodb

我正在尝试通过MongoDB Java Driver更新子文档。

我在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);

哪种方法正常。

现在我也在尝试更新SubDocument“文档”以将“下载”值设置为相同 - 我正在尝试这样的事情:

        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"}
        }
    });

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

1 个答案:

答案 0 :(得分:2)

如果这是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);