MongoDB 将字符串从两个字段连接到第三个字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12820253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB concatenate strings from two fields into a third field
提问by Yogesh Mangaj
How do I concatenate values from two string fields and put it into a third one?
如何连接两个字符串字段中的值并将其放入第三个字段?
I've tried this:
我试过这个:
db.collection.update(
{ "_id": { $exists: true } },
{ $set: { column_2: { $add: ['$column_4', '$column_3'] } } },
false, true
)
which doesn't seem to work though, and throws not ok for storage
.
但这似乎不起作用,并抛出not ok for storage
.
I've also tried this:
我也试过这个:
db.collection.update(
{ "_id": { $exists : true } },
{ $set: { column_2: { $add: ['a', 'b'] } } },
false, true
)
but even this shows the same error not ok for storage
.
但即使这样也显示相同的错误not ok for storage
。
I want to concatenate only on the mongo server and not in my application.
我只想在 mongo 服务器上连接,而不是在我的应用程序中连接。
采纳答案by William Z
Unfortunately, MongoDB currently does not allow you to reference the existing value of any field when performing an update(). There is an existing Jira ticket to add this functionality: see SERVER-1765for details.
不幸的是,MongoDB 当前不允许您在执行 update() 时引用任何字段的现有值。现有 Jira 票证可以添加此功能:有关详细信息,请参阅SERVER-1765。
At present, you must do an initial query in order to determine the existing values, and do the string manipulation in the client. I wish I had a better answer for you.
目前,您必须进行初始查询以确定现有值,并在客户端进行字符串操作。我希望我有一个更好的答案给你。
回答by rebe100x
回答by Nizam Khan
let suppose that you have a collection name is "myData" where you have data like this
假设你有一个集合名称是“myData”,你有这样的数据
{
"_id":"xvradt5gtg",
"first_name":"nizam",
"last_name":"khan",
"address":"H-148, Near Hero Show Room, Shahjahanpur",
}
and you want concatenate fields (first_name+ last_name +address) and save it into "address" field like this
并且您想要连接字段(名字+姓氏+地址)并将其保存到“地址”字段中,如下所示
{
"_id":"xvradt5gtg",
"first_name":"nizam",
"last_name":"khan",
"address":"nizam khan,H-148, Near Hero Show Room, Shahjahanpur",
}
now write query will be
现在写查询将是
{
var x=db.myData.find({_id:"xvradt5gtg"});
x.forEach(function(d)
{
var first_name=d.first_name;
var last_name=d.last_name;
var _add=d.address;
var fullAddress=first_name+","+last_name+","+_add;
//you can print also
print(fullAddress);
//update
db.myData.update({_id:d._id},{$set:{address:fullAddress}});
})
}
回答by Sagar Veeram
回答by Kenigmatic
Building on the answer from @rebe100x, as suggested by @Jamby ...
根据@Jamby 的建议,以@rebe100x 的答案为基础...
You can use $project, $concat and $out (or $merge) in an aggregation pipeline. https://docs.mongodb.org/v3.0/reference/operator/aggregation/project/https://docs.mongodb.org/manual/reference/operator/aggregation/concat/https://docs.mongodb.com/manual/reference/operator/aggregation/out/
您可以在聚合管道中使用 $project、$concat 和 $out(或 $merge)。 https://docs.mongodb.org/v3.0/reference/operator/aggregation/project/ https://docs.mongodb.org/manual/reference/operator/aggregation/concat/ https://docs.mongodb。 com/manual/reference/operator/aggregation/out/
For example:
例如:
db.collection.aggregate(
[
{ $project: { newfield: { $concat: [ "$field1", " - ", "$field2" ] } } },
{ $out: "collection" }
]
)
With MongoDB 4.2 . . .
使用 MongoDB 4.2 。. .
MongoDB 4.2 adds the $merge pipeline stage which offers selective replacement of documentswithin the collection, while $out would replace the entire collection. You also have the option of merging instead of replacing the target document.
MongoDB 4.2 添加了 $merge 管道阶段,它提供了集合中文档的选择性替换,而 $out 将替换整个集合。您还可以选择合并而不是替换目标文档。
db.collection.aggregate(
[
{ $project: { newfield: { $concat: [ "$field1", " - ", "$field2" ] } } },
{ $merge: { into: "collection", on: "_id", whenMatched: "merge", whenNotMatched: "discard" }
]
)
You should consider the trade-offs between performance, concurrency and consistency, when choosing between $merge and $out, since $out will atomically perform the collection replacementvia a temporary collection and renaming.
在 $merge 和 $out 之间进行选择时,您应该考虑性能、并发性和一致性之间的权衡,因为 $out 将通过临时集合和重命名原子地执行集合替换。
https://docs.mongodb.com/manual/reference/operator/aggregation/merge/https://docs.mongodb.com/manual/reference/operator/aggregation/merge/#merge-out-comparison
https://docs.mongodb.com/manual/reference/operator/aggregation/merge/ https://docs.mongodb.com/manual/reference/operator/aggregation/merge/#merge-out-comparison
回答by Hanoj B
db.collection.update( {"_id" :{"$exists":true}},[{"$set":{"column_2":{"$concat":["$column_4","$column_3"]}}}]
db.collection.update( {"_id" :{"$exists":true}},[{"$set":{"column_2":{"$concat":["$column_4","$column_3"]}}}]
in my case this $concat
worked for me ...
就我而言,这$concat
对我有用......
回答by ayyappa maddi
You can also follow the below.
您也可以按照以下步骤操作。
db.collectionName.find({}).forEach(function(row) {
row.newField = row.field1 + "-" + row.field2
db.collectionName.save(row);
});
回答by Cristhian Alejandro Pinto Jarr
db.myDB.find().forEach(function(e){db.myDB.update({"_id":e._id},{$set{"name":'More' + e.name + ' '}});
This is a solution!!
这是一个解决方案!!