S
S
Stanislav2018-07-15 18:31:22
MongoDB
Stanislav, 2018-07-15 18:31:22

How to perform concatenation directly in an array?

The collection has a photos field. When aggregating in $project, you need to reassemble the field to get it like this
['photo.jpg', 'photo2.jpg']

['photo.jpg?w=160&h=120', 'photo2.jpg?w=160&h=120']

There are no problems with the same field for getting a string:
photo: { $ifNull: [ { $concat: [ { $arrayElemAt: [ '$photos', 0 ] }, '?w=160&h=120' ]}, 'no-photo.jpg'] },

But you need to get an array of such strings

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cr2ed, 2018-07-16
@Cr2ed

['photo.jpg', 'photo2.jpg'].map(val => val.concat('?w=160&h=120'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question