使用 MongoOperation 在 Spring Data MongoDB 中构建 ArrayFilters 更新 [英] Building ArrayFilters update in Spring Data MongoDB with MongoOperation
问题描述
我正在使用 Spring Boot 2.2.1.RELEASE
.如何使用 Spring MongoOperation 编写以下 MongoDB 更新查询
I am using Spring boot 2.2.1.RELEASE
. How can I write the below MongoDB update query with Spring MongoOperation
db.cities.updateOne(
{
_id : ObjectId("5e78ec62bb5b406776e92fac")
},
{
$inc: {
"subscriptions.$[category].subscribers" : 1,
"subscriptions.$[category].options.$[option].subscribers" : 1
}
},
{ multi: true,
arrayFilters: [
{ "category._id": {$in: ["1", "2"]} },
{ "option.name": {$in: ["Time", "Gourmand", "Politics", "Entrepreneurship"]} }
]
}
)
我已经尝试了以下
Update update = new Update().inc("subscriptions.$[category].subscribers", 1).inc("subscriptions.$[category].options.$[option].subscribers", 1).filterArray(Criteria.where("category._id").in(Arrays.asList("1", "2")).andOperator(Criteria.where("option.name").in(Arrays.asList("Time", "Gourmand", "Politics", "Entrepreneurship"))));
UpdateResult result = mongoOperations.updateFirst(new Query(where("id").is(cityId)), update, CitiesDoc.class);
但我不断收到以下错误
org.springframework.dao.InvalidDataAccessApiUsageException: 命令失败,错误 9 (FailedToParse): 'Unrecognized field in update operation: arrayFilters' on server localhost:56740.完整的响应是 {"ok": 0.0, "errmsg": "Unrecognized field in update operation: arrayFilters", "code": 9, "codeName": "FailedToParse"};嵌套异常是 com.mongodb.MongoCommandException: 命令失败,错误 9 (FailedToParse): 'Unrecognized field in update operation: arrayFilters' on server localhost:56740.完整的响应是 {"ok": 0.0, "errmsg": "Unrecognized field in update operation: arrayFilters", "code": 9, "codeName": "FailedToParse"}
org.springframework.dao.InvalidDataAccessApiUsageException: Command failed with error 9 (FailedToParse): 'Unrecognized field in update operation: arrayFilters' on server localhost:56740. The full response is {"ok": 0.0, "errmsg": "Unrecognized field in update operation: arrayFilters", "code": 9, "codeName": "FailedToParse"}; nested exception is com.mongodb.MongoCommandException: Command failed with error 9 (FailedToParse): 'Unrecognized field in update operation: arrayFilters' on server localhost:56740. The full response is {"ok": 0.0, "errmsg": "Unrecognized field in update operation: arrayFilters", "code": 9, "codeName": "FailedToParse"}
不确定是哪个字段出现错误.一些建议会有所帮助.
Not sure for which field it is giving the error. Some suggestion will be helpful.
推荐答案
在 application.yaml
中添加以下内容:
Add the following to application.yaml
:
spring:
mongodb:
embedded:
version: 4.0.2
这篇关于使用 MongoOperation 在 Spring Data MongoDB 中构建 ArrayFilters 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!