MongoDB concatenate strings from two fields into a

2020-02-09 02:31发布

How do I concatenate values from two fields and put it into a third one, the values are strings. I've tried this:

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

doesn't seem to work though, throws not ok for storage. I've also tried this:

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

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

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

8条回答
\"骚年 ilove
2楼-- · 2020-02-09 03:12

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

in my case this $concat worked for me ...

查看更多
乱世女痞
3楼-- · 2020-02-09 03:15

You can also follow the below.

db.collectionName.find({}).forEach(function(row) { 
    row.newField = row.field1 + "-" + row.field2
    db.collectionName.save(row);
});
查看更多
叛逆
4楼-- · 2020-02-09 03:19

Building on the answer from @rebe100x, as suggested by @Jamby ...

You can use $project, $concat and $out (or $merge) in an aggregation pipeline. https://docs.mongodb.org/v3.0/reference/operator/aggregation/project/ https://docs.mongodb.org/manual/reference/operator/aggregation/concat/ https://docs.mongodb.com/manual/reference/operator/aggregation/out/

For example:

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

With MongoDB 4.2 . . .

MongoDB 4.2 adds the $merge pipeline stage which offers selective replacement of documents within the collection, while $out would replace the entire collection. You also have the option of merging instead of replacing the target document.

    db.collection.aggregate(
       [
          { $project: { newfield: { $concat: [ "$field1", " - ", "$field2" ] } } },
          { $merge: { into: "collection", on: "_id", whenMatched: "merge", whenNotMatched: "discard" }
       ]
    )

You should consider the trade-offs between performance, concurrency and consistency, when choosing between $merge and $out, since $out will atomically perform the collection replacement via a temporary collection and renaming.

https://docs.mongodb.com/manual/reference/operator/aggregation/merge/ https://docs.mongodb.com/manual/reference/operator/aggregation/merge/#merge-out-comparison

查看更多
该账号已被封号
5楼-- · 2020-02-09 03:24

let suppose that you have a collection name is "myData" where you have data like this

{
"_id":"xvradt5gtg",
"first_name":"nizam",
"last_name":"khan",
"address":"H-148, Near Hero Show Room, Shahjahanpur",
}

and you want concatenate fields (first_name+ last_name +address) and save it into "address" field like this

{
"_id":"xvradt5gtg",
"first_name":"nizam",
"last_name":"khan",
"address":"nizam khan,H-148, Near Hero Show Room, Shahjahanpur",
}

now write query will be

{
var x=db.myData.find({_id:"xvradt5gtg"});
x.forEach(function(d)
    { 
        var first_name=d.first_name;
        var last_name=d.last_name;
        var _add=d.address;  
        var fullAddress=first_name+","+last_name+","+_add; 
        //you can print also
        print(fullAddress); 
        //update 
        db.myData.update({_id:d._id},{$set:{address:fullAddress}});
    })
}
查看更多
走好不送
6楼-- · 2020-02-09 03:24

You could use $set like this in 4.2 which supports aggregation pipeline in update.

db.collection.update(
   {"_id" :{"$exists":true}},
   [{"$set":{"column_2":{"$concat":["$column_4","$column_3"]}}}]
)
查看更多
你好瞎i
7楼-- · 2020-02-09 03:31
db.myDB.find().forEach(function(e){db.myDB.update({"_id":e._id},{$set{"name":'More' + e.name + ' '}});

This is a solution!!

查看更多
登录 后发表回答