![](/img/trans.png)
[英]Spring data mongodb query add boolean field if value exists in array
[英]Project a boolean value with Spring from MongoDB depending on field existence
我需要根据使用 Spring Framework 的 MongoDB 中文档中某个字段的存在来投影一个布尔值。
假设我的 Mongo DB 中有文档。 它们的结构看起来像这样( testedField可能不存在):
{
"_id": ObjectId("some object ID"),
"field1": "some value",
"field2": "another value",
"testedField": "this field may be absent"
}
或者像这样:
{
"_id": ObjectId("some object ID"),
"field1": "some value",
"field2": "another value"
}
而且我还有一个数据类 MyClass:
@AllArgsConstructor
@Getter
public class MyClass {
private String field1;
private String field2;
private boolean myBoolVal;
}
在我的 DAO 中,有一种方法可以将其字段的值投影到类中:
public List<MyClass> findThings(Collection<ObjectId> ids) {
Criteria criteria = where(ID).in(ids);
Aggregation matchAndProject = newAggregation(
match(criteria),
project("field1", "field2")
.and("testedField").ne(null).as("myBoolVal"));
return mongoTemplate.aggregate(matchAndProject, "my_collection", MyClass.class).getMappedResults();
}
如果testsField存在,则在myBoolVal字段中设置为true
,如果不存在则设置为false
。
但是上面的代码抛出了IllegalArgumentException("Values must not be null!")
。
有没有办法让它工作?
使用条件使用条件投影
public List<MyClass> findThings(Collection<ObjectId> ids) {
Cond condition = ConditionalOperators.when(Criteria.where("testedField")
.exists(true))
.then(true)
.otherwise(false)
;
Criteria criteria = where(ID).in(ids);
Aggregation matchAndProject = newAggregation(
match(criteria),
project("field1", "field2")
and(condition).as("myBoolVal"));
return mongoTemplate.aggregate(matchAndProject, "my_collection", MyClass.class).getMappedResults();
}
为什么不是Boolean
? 如果字段存在,则为true
否则为false
和null
。
但是在您的情况下, myBoolVal
字段的任何内容都等于false
。 一件事 - 您的字段是最终的,您不能对它们进行任何操作......将实体字段设置为final
是不正确的。
最后一件事:你使用Spring吗? 它有一个与 mongodb 一起工作的完美库,并且与 mongodb 一起工作与 jpa 功能非常相似。
我找到了解决方案:只使用when("fieldName")
没有exists()
或is(null)
等就足够了。
public List<MyClass> findThings(Collection<ObjectId> ids) {
Criteria criteria = where(ID).in(ids);
Aggregation matchAndProject = newAggregation(
match(criteria),
project("field1", "field2")
.and(ConditionalOperators.when("testedField")
.then(true)
.otherwise(false))
.as("myBoolVal"));
return mongoTemplate.aggregate(matchAndProject, "my_collection", MyClass.class).getMappedResults();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.