3

I am novice at MongoDB 3.2,

Consider below example,

{ "_id" : "2tLX8ALYfRvbgiurZ", "service" : "GSTR 1" }
{ "_id" : "2tLX8ALYfRvbgiurZ", "service" : "GSTR 2" }
{ "_id" : "2tLX8ALYfRvbgiurZ", "service" : "GSTR 3" }

How can I use Group By _id and create single field with all document fields being concatenated, below is expected output,

{ "_id" : "2tLX8ALYfRvbgiurZ", "service" : "GSTR 1, GSTR 2, GSTR 3" }

I used below but it gives array,

db.getCollection('Clients').aggregate(
      [
        {
            $group : {
             _id : "$_id",
             services :  "$services"
            }
        }
      ]
    ).map( doc =>
      Object.assign(
        doc,
       { "services": doc.services.join(",") }
      )
    );

it gives output as

[{ "_id" : "2tLX8ALYfRvbgiurZ", "service" : "GSTR 1, GSTR 2, GSTR 3" }]
Ankur Soni
  • 5,725
  • 5
  • 50
  • 81
  • 1
    Possible duplicate of [Concat String by Group](https://stackoverflow.com/questions/46841825/concat-string-by-group) – Aakash Verma Nov 27 '17 at 15:25
  • 1
    Possible duplicate of [Concatenate string values in array in a single field in MongoDB](https://stackoverflow.com/questions/28882552/concatenate-string-values-in-array-in-a-single-field-in-mongodb) – dnickless Nov 27 '17 at 21:08
  • @dnickless: The implementation in your suggested link is in mongo db 3.4 – Ankur Soni Nov 28 '17 at 05:56

2 Answers2

6

It's working fine i checked

db.client.insert([
    { "id" : "2tLX8ALYfRvbgiurZ", "service" : "GSTR 1" },
    { "id" : "2tLX8ALYfRvbgiurZ", "service" : "GSTR 2" },
    { "id" : "2tLX8ALYfRvbgiurZ", "service" : "GSTR 3" }
]);
db.getCollection('client').aggregate(
    [
        {
            $group: {
                _id:"$id",
                myfield: {$push: {$concat: ["$service"]}}
            }
        },
        {
            $project:{
                "results": {
                    $reduce: {
                        input: "$myfield",
                        initialValue: '',
                        in: {$concat: ["$$value", ", ", "$$this"]}
                    }
                }
            }
        }
    ]
);
Ikar Pohorský
  • 4,617
  • 6
  • 39
  • 56
manish
  • 81
  • 1
  • 7
  • You can also add ` '$cond': [ { '$eq': [ '$$value', '' ] }, '', ', ' ]` in the `$concat` to omit any leading ", " – user2402616 May 19 '21 at 16:23
0
docIwant = db.getCollection('Clients').aggregate(
      [
        {
            $group : {
             _id : "$_id",
             services :  "$services"
            }
        }
      ]
    ).map( doc =>
      Object.assign(
        doc,
       { "services": doc.services.join(",") }
      )
    );
return docIwant[0].
Israel Zinc
  • 2,713
  • 2
  • 18
  • 30
  • `$push` creates array, I need concatenated String. – Ankur Soni Nov 28 '17 at 06:06
  • I would risk to say that that's not possible using mongodb solo can't be used for this task. I did a fast research in concatenations using projections but I could not find an answer for that. Maybe you could use your favorite programming language to concatenate after the query. If it's in python I can give you a hand. – Israel Zinc Nov 28 '17 at 06:12
  • @isael.zinc : thanks a lot. I am using javascript :) – Ankur Soni Nov 28 '17 at 06:22
  • If my answer answered your question, please accept it. – Israel Zinc Nov 28 '17 at 06:33