Spring-Mongo-Data Update 只允许一个位置参数? [英] Spring-Mongo-Data Update only allows one positional argument?

查看:78
本文介绍了Spring-Mongo-Data Update 只允许一个位置参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的数据模型:

<代码>{_id: ObjectId(''),字符串: [{_id: ObjectId(''),nds:[{_id: ObjectId(''),标题: ''},...]},...]}

以下查询在 mongo shell 中运行良好:

mongo.update({_id: ObjectId(''), 'strs._id': ObjectId(''), 'strs.nds._id': ObjectId('')}, {$set:{'strs.$.nds.$.title': 'new-title'}})

我正在尝试在 Spring Boot 中做同样的事情,我编写了以下代码行:

Criteria criteria = new Criteria();标准.addOperator(Criteria.where("id").is(id),Criteria.where("strs.id").is(strsId), Criteria.where("strs.nds.id", ndsId));查询查询 = new Query().addCriteria(criteria);更新更新 = 新更新();update.set("strs.$.nds.$.title", title);mongoTemplate.findAndModify(query, update, MyModel.class);

但是,这并没有按预期工作.据说 mongo 无法在 [nds: {...}] 内创建 title 字段.因此,我记录了 MongoTemplate 生成的查询,结果证明 MongoTemplate 正在从查询中删除第二个位置参数 $.

这是生成的查询:

mongo.update({...}, {$set: {'strs.$.nds.title': 'new-title'}})

这就是 mongo 抛出异常的原因,说它不能在数组中创建 title 字段.

我这样做错了吗?因为 MongoTemplate 正在生成一个无效的查询,该查询失败(显然).

解决方案

你能做的是

update.set("strs.$[elmStr].nds.$[elmNds].title", title).filterArray("elmStr._id", strsId).filterArray("elmNds._id",ndsId);

并参考位置运算符

I have a data model with the following structure:

{
    _id: ObjectId(''),
    strs: [
        {
            _id: ObjectId(''),
            nds: [
                {
                    _id: ObjectId(''),
                    title: ''
                },
                .
                .
                .
            ]
        },
        .
        .
        .
    ]
}

Following query works perfectly fine in mongo shell:

mongo.update({_id: ObjectId(''), 'strs._id': ObjectId(''), 'strs.nds._id': ObjectId('')}, {$set: {'strs.$.nds.$.title': 'new-title'}})

I am trying to do the same in Spring Boot, I have written the following lines of code:

Criteria criteria = new Criteria();
criteria.addOperator(Criteria.where("id").is(id), 
    Criteria.where("strs.id").is(strsId), Criteria.where("strs.nds.id", ndsId));
Query query = new Query().addCriteria(criteria);
Update update = new Update();
update.set("strs.$.nds.$.title", title);
mongoTemplate.findAndModify(query, update, MyModel.class);

But, this is not working as expected. It's saying that mongo cannot create a title field inside [nds: {...}]. So, I logged the queries MongoTemplate was generating, and it turns out that MongoTemplate was removing the second positional argument $ from the query.

This was the generated query:

mongo.update({...}, {$set: {'strs.$.nds.title': 'new-title'}})

And this was the reason mongo was throwing an exception saying it cannot create a title field in an array.

Am I doing this wrong? Because MongoTemplate is generating an invalid query which is failing (obviously).

解决方案

What you can do is

update.set("strs.$[elmStr].nds.$[elmNds].title", title)
    .filterArray("elmStr._id", strsId)
    .filterArray("elmNds._id",ndsId);

And refer positional operator

这篇关于Spring-Mongo-Data Update 只允许一个位置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆