MongoDB:使用$ concat更新字段值时出现问题

编程入门 行业动态 更新时间:2024-10-07 10:23:46
本文介绍了MongoDB:使用$ concat更新字段值时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正尝试通过将其与文字字符串连接来更新MongoDB集合中字段的值。除此之外,该字段是一个整数,我想在前面添加一个 0,因此它将成为一个字符串。

I'm trying to update the value of a field in a MongoDB collection by concatenating it with a literal string. Besides this, the field is an integer, and I want to add a "0" in front, so it will became a string.

我已经读到可以不会在单个更新指令中使用字段的旧值,因此我使用的是 forEach()方法。

I've read that I can't use the old value of the field in a single update instruction, so I'm using a forEach() method.

这里是代码:

db.col_1.find({"field_1": {$lt: 10000}}).forEach( function(i){ db.col_1.update({_id: i._id}, {$set: { "field_1": {$concat: ["0", i.field_1]}}} ) });

返回结果为:

The dollar ($) prefixed field '$concat' in 'field_1.$concat' is not valid for storage.

我确定我没有正确编写$ concat命令,有什么办法

I'm sure I'm not writting the $concat command properly, is there any way to do this?

推荐答案

$ concat是聚合管道,而不是更新运算符/修饰符。

$concat is an aggregation pipeline, not an update operator/modifier.

似乎您可以通过以下操作来实现:

It seems that what you're trying to do can be achieved by doing the following:

db.col_1 .find({ "field_1": { $lt: 10000 } }) .forEach( function(i) { db.col_1.update( { _id: i._id }, { $set: { "field_1": "0" + i.field_1 } } ) });

更多推荐

MongoDB:使用$ concat更新字段值时出现问题

本文发布于:2023-10-23 02:30:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   MongoDB   concat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!