[英]Use $Concat with UpdateOne to update the subobject string field Java
I'm using mongo-java-driver for mongodb connection and wanted to update one field in subobject of the record.我正在使用 mongo-java-driver 进行 mongodb 连接,并想更新记录子对象中的一个字段。
Record:记录:
{
"_id": "61866c32d11c9aefb01125f7",
"number": 1900,
"oms": "ters",
"data": [
{
"empCode": "EMP2",
"firstname": "vivek",
"lastname": "bdh",
"age": 30
},
{
"empCode": "EMP3",
"firstname": "ravi",
"lastname": "sh",
"age": 30
}
],
"volgnummer": 1
}
I wanted to update lastname where empCode = "EMP2".我想更新 empCode = "EMP2" 的姓氏。
Following is my code snippet to perform the operation以下是我执行操作的代码片段
public class FixDataApp {
public static void main(String[] args) throws IOException {
MongoCollection collection = MongoConfig.getCollection(args[0], args[1], "myCollection");
BasicDBObject concat = new BasicDBObject();
concat.put("$concat", Arrays.asList(
new BsonString("data.$.lastname"),
new BsonString(" TestAppendStr")));
BasicDBObject updateFields = new BasicDBObject();
updateFields.append( "data.$.lastname", concat);
BasicDBObject command = new BasicDBObject();
command.put("$set", updateFields);
UpdateResult result = collection.updateOne(
Filters.and(
Filters.eq("_id", "61866c32d11c9aefb01125f7"),
Filters.eq("data.empCode", "EMP2")
),
command
);
System.out.println("Record updated successfully " + result.getModifiedCount());
}
}
When I'm executing the above application getting following error...当我执行上述应用程序时出现以下错误......
Exception in thread "main" com.mongodb.MongoWriteException: The dollar ($) prefixed field '$concat' in 'data.0.lastname.$concat' is not valid for storage.
at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1060)
at com.mongodb.client.internal.MongoCollectionImpl.executeUpdate(MongoCollectionImpl.java:1037)
at com.mongodb.client.internal.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:622)
at com.mongodb.client.internal.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:617)
at common.FixDataApp.main(FixDataApp.java:30)
Can anyone help to update such field with $concat and update method.任何人都可以使用 $concat 和 update 方法帮助更新此类字段。 Thanks
谢谢
A bit complex MongoDB update query.有点复杂的 MongoDB 更新查询。
As you are updating a document in the array field and at the same time you are using $concat
, which requires an aggregation pipeline.当您在数组字段中更新文档并同时使用
$concat
时,这需要一个聚合管道。
Without $concat
(aggregation pipeline), you can use arrayFilters
and $[elem]
which can be straight and easier to update desired document field in the nested array.如果没有
$concat
(聚合管道),您可以使用arrayFilters
和$[elem]
可以直接且更容易地更新嵌套数组中所需的文档字段。
The main points in the below query:以下查询中的要点:
$map
- Iterate and update for each document in data
array field. $map
- 迭代和更新data
数组字段中的每个文档。
$cond
- When meets requirement, the lastname
field will be updated with the value from $concat
. $cond
- 当满足要求时, lastname
字段将使用$concat
中的值进行更新。 Else it will be updated with the original value.否则它将使用原始值进行更新。
$mergeObjects
- Merge the original document $emp
with new updated field, lastname
. $mergeObjects
- 将原始文档$emp
与新的更新字段lastname
合并。
db.collection.update({
"_id": "61866c32d11c9aefb01125f7",
"data.empCode": "EMP2"
},
[
{
$set: {
"data": {
"$map": {
"input": "$data",
"as": "emp",
"in": {
$mergeObjects: [
"$$emp",
{
"lastname": {
$cond: {
if: {
$eq: [
"$$emp.empCode",
"EMP2"
]
},
then: {
$concat: [
"$$emp.lastname",
" TestAppendStr"
]
},
else: "$$emp.lastname"
}
}
}
]
}
}
}
}
}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.