mongodb aggregation, is it possible to add entries to an array during $project

Viewed 5159

I have documents of the form:

{
    _id : ObjectId(.....),
    prop1 : "foo",
    links : [ 1, 2, 3, 4 ]
}

{
    _id : ObjectId(.....),
    prop1 : "bar",
    links : [ 5, 6, 7, 8 ]
}

I am using the aggregation framework to process these documents, I use $unwind to generate a document for each value in the links array.

But I have three cases where I need to update the documents before calling $unwind, I have been looking at the $project operation, but I can find no information about how to create or update arrays for the following cases.

1) The links property is missing

{
    _id : ObjectId(.....),
    prop1 : "far"
}

I need to insert the links array

2) The links array property is an empty array

{
    _id : ObjectId(.....),
    prop1 : "far",
    links : []
}

I need to insert a value into the array

3) The links array has too few values

{
    _id : ObjectId(.....),
    prop1 : "far",
    links : [ 9, 10 ]
}

I need to insert additional values into the array

1 Answers
Related