Jaganmohanreddy
Jaganmohanreddy

Reputation: 431

How to convert string array to string by keeping the contents of array as comma separated values in MongoDB 4.0

I have collection which has an array field as follows.

availableProducts: [ "mobile", "laptop", "desktop"]

Now I would like to update this field to a string field at the same time i need to keep the values of array as comma separated strings as follows

availableProducts: "mobile,laptop,desktop"

Can we do this with almost 1 million records in MongoDB version 4.0

Upvotes: 6

Views: 5756

Answers (3)

Ilya Sh
Ilya Sh

Reputation: 111

Much easier and understandable way:

db.collection.aggregate([
  {
    $addFields: {
      "availableProducts": {
        $reduce: {
          input: "$availableProducts",
          initialValue: "",
          in:{
            $concat:[
               "$$value",
               {
                  "$cond":{
                     "if":{
                        "$eq":[
                           "$$value",
                           ""
                        ]
                     },
                     "then":"",
                     "else":","
                  }
               },
               "$$this"
            ]
         }
        }
      }
    }
  },
  {$out:'collection'}
])

Source: https://docs.mongodb.com/manual/reference/operator/aggregation/reduce/

Upvotes: 6

prasad_
prasad_

Reputation: 14287

You can do this from mongo shell, as follows:

Input documents:

{ "_id" : 1, "a" : [ "blue", "green", "red" ] }
{ "_id" : 2, "a" : [ "cat", "dog", "rat" ] }

The query:

db.arr1.find()
       .forEach( doc => { doc.a = doc.a.toString(); db.arr1.save(doc); } )

Result (the updated collection):

{ "_id" : 1, "a" : "blue,green,red" }
{ "_id" : 2, "a" : "cat,dog,rat" }

Upvotes: 2

matthPen
matthPen

Reputation: 4353

You can achieve this with the following aggregation :

db.collection.aggregate([
  {
    $addFields: {
      "availableProducts": {
        $reduce: {
          input: "$availableProducts",
          initialValue: "",
          in: {
            "$cond": {
              if: {
                "$eq": [
                  {
                    "$indexOfArray": [
                      "$availableProducts",
                      "$$this"
                    ]
                  },
                  0
                ]
              },
              then: {
                "$concat": [
                  "$$value",
                  "$$this"
                ]
              },
              else: {
                "$concat": [
                  "$$value",
                  ",",
                  "$$this"
                ]
              }
            }
          }
        }
      }
    }
  },
  {$out:'collection'}
])

Use it carefully, as the $out stage will replace your entire collection with the result. My advice is to test without $out stage before.

You can test it here

Upvotes: 0

Related Questions