簡體   English   中英

MongoDB Java驅動程序更新子文檔

[英]MongoDB Java Driver Update Sub-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);

哪個工作正常。

現在,我還嘗試更新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 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM