1

I have the following collection

{
  "_id": "12345",
  "Products": [
    "Product1",
    "Product2"
  ],
  "State": "active",
}

And would like to project it similar to the output below

_id: "12345"
Products: "Product1, product2"

I tried this but im not getting the desired output of a comman seperated concatenated string

{
  _id: 1,
  Products: { 
      $reduce: { 
          input: "$Products", 
          initialValue:"0" ,
          in: {concat:  ["$$value", "$$this"] } 
      }
  }
}
6
  • It not clear from your formatting what Products is. Is it an array of objects so Products: [{ '0': 'Product1'}, { '1': 'product2'}] or is it an array of just strings so Products: ['Product1', 'product2'] ? It would help if you post an actual sample document. Commented Apr 1, 2024 at 20:44
  • @jQueeny Ive updated the question based on your observation Commented Apr 1, 2024 at 21:07
  • Would this be what you're looking for? The $cond is needed becuase you don't want a leading or a trailing comma in the output I assume. Commented Apr 1, 2024 at 21:30
  • Thanks @jQueeny. Thats exactly what I wanted. Commented Apr 1, 2024 at 21:43
  • I'll put as an answer then. Commented Apr 1, 2024 at 21:48

2 Answers 2

2

This aggregation query gives the desired result:

db.test.aggregate([{
    $project: {
        Products: {
            $ltrim: {
                input: {
                    $reduce: {
                        input: "$Products",
                        initialValue: "",
                        in: {
                            $concat: [ "$$value", ", ", "$$this" ]
                        }
                    }
                },
                chars: ", "
            }
        }
    }
}])

The $project is used to transform the Products array of strings into a single concatenated string, using the $reduce array operator.

Note the usage of the $ltrim operator to remove the addition comma from the concatenated string, to give the following result:

[ 
  { 
    _id: '12345', 
     Products: 'Product1, Product2' 
  } 
]

The projection of _id field is not explicitly required, it is by default projected.

Sign up to request clarification or add additional context in comments.

1 Comment

Think I prefer the $ltrim. This is how I would do it programmatically and would prevent the $cond execution on each iteration of reduce element.
1

You can use $reduce and $concat to concatenate the values into one string. The $cond is necessary to determine if a comma is required between the values or not.

db.collection.aggregate([
  {
    $project: {
      "_id": 1,
      "Products": {
        $reduce: {
          input: "$Products",
          initialValue: "",
          in: {
            $concat: [
              "$$value",
              {
                $cond: [
                  {
                    $eq: [
                      "$$value",
                      ""
                    ]
                  },
                  "",
                  ","
                ]
              },
              "$$this"
            ]
          }
        }
      }
    }
  }
])

See HERE for a working example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.