[英]MongoDB Java - UpdateMany() with both an $inc operator and $concat aggregation
JavaVersion: 11 --- MongoDB-driver: 4.6.1 JavaVersion:11 --- MongoDB 驱动程序:4.6.1
I'm wondering if it is possible to implement a bulk update method that would increment the document version through the $inc operator while simultaneously making it possible to concatenate a string to a field value through $concat aggregation .我想知道是否可以实现一个批量更新方法,该方法将通过$inc 运算符增加文档版本,同时可以通过$concat 聚合将字符串连接到字段值。
I've tried some workarounds, but I cannot manage the whole thing to work, in particular:我尝试了一些解决方法,但我无法管理整个工作,特别是:
Combining all the operators in a single Bson with Updates::combine -> $concat and $inc works independently, but combining both of them I get this error: "A pipeline stage specification object must contain exactly one field."将单个 Bson 中的所有运算符与 Updates::combine -> $concat 和 $inc 独立工作,但将它们结合起来我得到这个错误: “管道阶段规范 object 必须包含一个字段。”
Passing a List of Bson to "updateMany() -> I get no error, but $concat does not work as expected, in the DB I have something like:将 Bson 列表传递给“updateMany() -> 我没有收到错误,但是 $concat 没有按预期工作,在数据库中我有类似的东西:
"field1": {"$concat": ["$field1", "_OK"]},
In order to make $concat work I use this (according to the documentation):为了使 $concat 工作,我使用它(根据文档):
Updates.set(new Document("$concat", Arrays.asList(
new BsonString(String.format("$%s", key)),
new BsonString(value))));
While for $inc I'm using the Updetes.inc() method.而对于 $inc 我正在使用 Updetes.inc() 方法。
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ////////////
Here is the resulting Update object from the 1 and 2 strategies:这是从 1 和 2 策略生成的更新 object:
List of BSON BSON列表
[
Update
{
fieldName='field1',
operator='$set',
value=Document
{
{
$concat=
[
BsonString{value='$field1'},
BsonString{value='_OK'}
]
}
}
},
Update
{
fieldName='field2',
operator='$set',
value=2022-08-29T14:16:07.931Z
},
Updates{
updates=[
Update
{
fieldName='documentId.version',
operator='$inc',
value=1
},
Update
{
fieldName='documentId.objId',
operator='$set',
value=c4c93c42-1007-4b86-85e4-a45396b9ba0c
},
Update
{
fieldName='updatedTimestamp',
operator='$set',
value=2022-08-29T14:16:07.931Z
}
]
}
]
Combining all the operands合并所有操作数
Updates
{
updates=
[
Updates
{
updates=
[
Update
{
fieldName='field1',
operator='$set',
Document
{
{
$concat=
[
BsonString{value='$field1'},
BsonString{value='_OK'}
]
}
}
},
Update
{
fieldName='field2',
operator='$set',
value=2022-08-29T14:30:49.269Z
}
]
},
Updates
{
updates=
[
Update
{
fieldName='documentId.version',
operator='$inc',
value=1},
Update
{
fieldName='documentId.objId',
operator='$set',
value=cdf5f0b7-5832-40cc-88e5-a71172afa58a
},
Update
{
fieldName='updatedTimestamp',
operator='$set',
value=2022-08-29T14:30:49.269Z
}
]
}
]
}
Solved解决了
As pointed out by prasad_ in the comments, you can't combine update operators with pipeline ones.正如评论中的prasad_所指出的,您不能将更新运算符与管道运算符结合使用。 To solve the issue, I've used a List of BSON using $add aggregation instead of the $inc operator.为了解决这个问题,我使用了一个使用 $add 聚合而不是 $inc 运算符的 BSON 列表。
An implementation idea for the $add operator. $add 操作符的一个实现思路。
Updates.set(key, new Document("$add", Arrays.asList(
new BsonString(key),
value)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.