Yog*_*gaj 15 string-concatenation mongodb mongodb-query mongodb-update aggregation-framework
如何连接两个字段中的值并将其放入第三个字段中,值为字符串.我试过这个:
db.collection.update({"_id" : { $exists : true }},
{$set: {column_2:{$add:['$column_4',
'$column_3']}}},
false, true)
Run Code Online (Sandbox Code Playgroud)
虽然似乎不起作用,但是抛出not ok for storage.我也试过这个:
db.collection.update({"_id" : { $exists : true }},
{$set: {column_2:{$add:['a',
'b']}}},
false, true)
Run Code Online (Sandbox Code Playgroud)
但即使这显示同样的错误not ok for storage.
我想只在mongo服务器上连接而不是在我的应用程序中连接.
reb*_*00x 17
你可以使用aggregate,$ project和$ concat:https : //docs.mongodb.org/v3.0/reference/operator/aggregation/project/ https://docs.mongodb.org/manual/reference/operator/aggregation/CONCAT /
它会是这样的:
db.collection.aggregate(
[
{ $project: { newfield: { $concat: [ "$field1", " - ", "$field2" ] } } }
]
)
Run Code Online (Sandbox Code Playgroud)
不幸的是,MongoDB目前不允许您在执行update()时引用任何字段的现有值.现有的Jira票证可添加此功能:有关详细信息,请参阅SERVER-1765.
目前,您必须执行初始查询以确定现有值,并在客户端中执行字符串操作.我希望我能给你一个更好的答案.
您可以在 4.2 中使用$set这样的方式,它支持更新中的聚合管道。
db.collection.update(
{"_id" :{"$exists":true}},
[{"$set":{"column_2":{"$concat":["$column_4","$column_3"]}}}]
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20205 次 |
| 最近记录: |