MongoDB 将两个字段中的字符串连接到第三个字段中 [英] MongoDB concatenate strings from two fields into a third field

查看:17
本文介绍了MongoDB 将两个字段中的字符串连接到第三个字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I concatenate values from two string fields and put it into a third one?

我试过了:

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

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

which doesn't seem to work though, and throws not ok for storage.

我也试过这个:

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

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

but even this shows the same error not ok for storage.

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

I want to concatenate only on the mongo server and not in my application.

推荐答案

不幸的是,MongoDB 目前不允许您在执行 update() 时引用任何字段的现有值.有一个现有的 Jira 票可以添加此功能:有关详细信息,请参阅 SERVER-1765.

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-1765 for details.

目前,您必须进行初始查询以确定现有值,并在客户端中进行字符串操作.我希望我能给你一个更好的答案.

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.

这篇关于MongoDB 将两个字段中的字符串连接到第三个字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆