我有一个这种格式的文件:
{
"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);
即使 executionParam1 是 Apple, 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 修饰符的位置运算符来更新查询过滤器中的字段匹配。
以下查询将更新executionParam1
为Banana
.
就像是
this.mongoDailyReportCollection.updateOne(filter, Updates.set("daily.$.executionParam1", "Banana"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句