Suppose that I have a serie of documents with the following format:
{
"_id": "3_0",
"values": ["1", "2"]
}
and I would want to obtain a projection of the values in the array concatenated in a single field. I would want to obtain each document with the following format.
{
"_id": "3_0",
"values": "1_2"
}
Is this possible? I have tried with $concat
but I guess that I can't use $values
as the array for $concat
.
You can use the reduce operator together with the substr operator.
In Modern MongoDB releases you can. You still cannot "directly" apply an array to
$concat
, however you can use$reduce
to work with the array elements and produce this:Combining of course with
$indexOfArray
in order to not "concatenate" with the"_"
underscore when it is the "first" index of the array.Also my additional "wish" has been answered with
$sum
:This kind of gets raised a bit in general with aggregation operators that take an array of items. The distinction here is that it means an "array" of "aguments" provided in the coded representation a opposed to an "array element" present in the current document.
The only way you can really do the kind of concatenation of items within an array present in the document is to do some kind of JavaScript option, as with this example in mapReduce:
Of course if you are not actually aggregating anything, then possibly the best approach is to simply do that "join" operation within your client code in post processing your query results. But if it needs to be used in some purpose across documents then mapReduce is going to be the only place you can use it.
I could add that "for example" I would love for something like this to work:
And in aggregate:
But it does not work that way because
$add
expects arguments as opposed to an array from the document. Sigh! :(. Part of the "by design" reasoning for this could be argued that "just because" it is an array or "list" of singular values being passed in from the result of the transformation it is not "guaranteed" that those are actually "valid" singular numeric type values that the operator expects. At least not at the current implemented methods of "type checking".That means for now we still have to do this:
And also sadly there would be no way to apply such a grouping operator to concatenate strings either.
So you can hope for some sort of change on this, or hope for some change that allows an externally scoped variable to be altered within the scope of a
$map
operation in some way. Better yet a new$join
operation would be welcome as well. But these do not exist as of writing, and probably will not for some time to come.