2017-06-16 114 views
0

我在這個格式的文檔:MongoDB的投影不工作的Java

{ 
"env" : "local", 
...., 
"daily" : [ 
    { 
    "executionParam1" : "Apple", 
    "executionParam2" : "sour", 
    "executionParam3" : "today", 
    ... 
    }, 
    { 
    "executionParam1" : "Oranges", 
    "executionParam2" : "sour", 
    "executionParam3" : "tomorrow", 
    .... 
    }... 
] 

我使用MongoDB的Java驅動程序查詢。該查詢是這種形式:

this.mongoDailyReportCollection = this.mongoDb.getCollection("environments"); 
Bson projection = fields(excludeId(), 
           include("env", "daily"), 
           Projections.elemMatch("daily", 
                 and(eq("executionParam1", coll.getexecutionParam1()), 
                  eq("executionParam2", coll.getexecutionParam2()), 
                  eq("executionParam3", coll.getexecutionParam3())))); 
long count = this.mongoDailyReportCollection.count(projection); 

我不斷收到計數爲0,即使executionParam1是蘋果,executionParam2是又酸又executionParam3是今天。如果我想更新與此相匹配的文檔,那麼過程如何?

回答

0

您正在將投影文檔發送到接受查詢文檔的count方法。

Bson filter = Filters.elemMatch("daily", and(eq("executionParam1", coll.getexecutionParam1()), eq("executionParam2", coll.getexecutionParam2()), eq("executionParam3", coll.getexecutionParam3()))); 

long count = this.mongoDailyReportCollection.count(filter); 

您將使用位置運算符和set修飾符來更新查詢過濾器中的字段匹配。

以下查詢將更新executionParam1Banana

喜歡的東西

this.mongoDailyReportCollection.updateOne(filter, Updates.set("daily.$.executionParam1", "Banana"));