MongoDB: Problems using $concat to update the value of a field
Asked Answered
S

3

8

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.

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.

Here is the code:

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 return result is :

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

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

Snowshoe answered 16/1, 2017 at 11:8 Comment(4)
Try {$set: { "field_1": "0" + i.field_1 } }Tourbillion
$concat can only be used in aggregation queriesPolymorphonuclear
@Tourbillion -- It works perfectly well like this, thank you very much!Snowshoe
Answer posted. Please approve and upvote my answer if you like my effortHigginson
R
10

$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 } }
    )
   });
Reporter answered 2/8, 2018 at 15:29 Comment(0)
H
6

To update the MongoDB field using the value of another field for MongoDB version 4.2 also introduced the $set pipeline stage operator which is an alias for $addFields. You can use $set here as it maps with what we are trying to achieve.

let query = {
    "field_1": {
        $lt: 10000
    }
};
let changes = {
    $set: {
        "field_1": {
            "$concat": ["0", "$field_1"]
        }
    }
};
let UpdatedStatus = await col_1.updateMany(query, [changes]).lean();
console.log(UpdatedStatus);
Higginson answered 14/3, 2020 at 4:26 Comment(5)
No need for using time taking looping processHigginson
I didn't know we could use pipelines in update operations just like with aggregations, the more I know mongodb, the more I like it!Medeiros
This will not work, you cannot use $concat in an insert/update statementRosemari
from version 4.2 u can use aggregate operators in update with changes in array [] instead of object{}Higginson
This is by far the best option. Thanks. you also solved the problem of the need to pass the "change" part with "[ ]" parenthesis. Note, you can also add "toString" to the contact option: "$concat": [value, "_", {"$toString": "$_id"}].Perithecium
P
2

Here is another way if you want to use $set with $concat:

db.col_1.updateMany(
    {"field_1": {$lt: 10000}},
    [
        { $set: { "field_1": {$concat: ["0", "$field_1"]} } }
    ]
 )

You don't need to use forEach. With updateMany, you can easily modify all the documents that match the query {"field_1": {$lt: 10000}}.

Preeminence answered 27/10, 2023 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.