gpt4 book ai didi

MongoDB 将两个字段中的字符串连接到第三个字段中

转载 作者:IT老高 更新时间:2023-10-28 13:11:29 25 4
gpt4 key购买 nike

如何连接两个字符串字段中的值并将其放入第三个字段?

我试过了:

db.collection.update(
{ "_id": { $exists: true } },
{ $set: { column_2: { $add: ['$column_4', '$column_3'] } } },
false, true
)

这似乎不起作用,并抛出 not ok for storage

我也试过这个:

db.collection.update(
{ "_id": { $exists : true } },
{ $set: { column_2: { $add: ['a', 'b'] } } },
false, true
)

但即使这样也显示相同的错误not ok for storage

我只想在 mongo 服务器上而不是在我的应用程序中连接。

最佳答案

您可以使用聚合运算符 $project$concat :

db.collection.aggregate([
{ $project: { newfield: { $concat: [ "$field1", " - ", "$field2" ] } } }
])

关于MongoDB 将两个字段中的字符串连接到第三个字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12820253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com