2017-06-16 69 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"));