我有一个以下查询,我想在其中查找员工信息,但我只想包括几个字段。
字段landline
和mobile
是可选的,因此它们可能存在也可能不存在。
Query query = new Query();
query.addCriteria(Criteria.where("name").is(name)
.orOperator(
Criteria.where("landline").exists(true),
Criteria.where("mobile").exists(true)
)
);
query.fields()
.include("name")
.include("landline")
.include("mobile")
return mongoTemplate.find(query, Employee.class);
我想退回所有员工,每个员工都应仅包括姓名,座机,移动字段。 如果存在固定电话/移动电话,则应将另一个设置为空。
上面的示例抛出以下错误:
原因: org.springframework.data.mapping.context.InvalidPersistentPropertyPath: 在com.abc.xyz.Employee上找不到财产移动电话
如果我删除了query.fields.include()
,它将成功返回雇员以及所有字段。