Include all existing fields and add new fields to document

MongodbMongodb QueryAggregation Framework

Mongodb Problem Overview


I would like to define a $project aggregation stage where I can instruct it to add a new field and include all existing fields, without having to list all the existing fields.

My document looks like this, with many fields:

{
    obj: {
        obj_field1: "hi",
        obj_field2: "hi2"
    },
    field1: "a",
    field2: "b",
    ...
    field26: "z"
}

I want to make an aggregation operation like this:

[
    {
        $project: {
            custom_field: "$obj.obj_field1",
            //the next part is that I don't want to do
            field1: 1,
            field2: 1,
            ...
            field26: 1
        }
    },
    ... //group, match, and whatever...
]

Is there something like an "include all fields" keyword that I can use in this case, or some other way to avoid having to list every field separately?

Mongodb Solutions


Solution 1 - Mongodb

In 4.2+, you can use the $set aggregation pipeline operator which is nothing other than an alias to $addFieldsadded in 3.4

>The $addFields stage is equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

db.collection.aggregate([
    { "$addFields": { "custom_field": "$obj.obj_field1" } }
])

Solution 2 - Mongodb

You can use $$ROOT to references the root document. Keep all fields of this document in a field and try to get it after that (depending on your client system: Java, C++, ...)

 [
    {
        $project: {
            custom_field: "$obj.obj_field1",
            document: "$$ROOT"

        }
    },
    ... //group, match, and whatever...
]

Solution 3 - Mongodb

>>> There's something like "include all fields" keyword that I can use in this case or some another solution?

Unfortunaly, there is no operator to "include all fields" in aggregation operation. The only reason, why, because aggregation is mostly created to group/calculate data from collection fields (sum, avg, etc.) and return all the collection's fields is not direct purpose.

Solution 4 - Mongodb

To add new fields to your document you can use $addFields

from docs

and to all the fields in your document, you can use $$ROOT

db.collection.aggregate([

{ "$addFields": { "custom_field": "$obj.obj_field1" } },
{ "$group": {
        _id : "$field1",
        data: { $push : "$$ROOT" }
    }}
])

Solution 5 - Mongodb

As of version 2.6.4, Mongo DB does not have such a feature for the $project aggregation pipeline. From the docs for $project:

> Passes along the documents with only the specified fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.

and

> The _id field is, by default, included in the output documents. To include the other fields from the input documents in the output documents, you must explicitly specify the inclusion in $project.

Solution 6 - Mongodb

according to @Deka reply, for c# mongodb driver 2.5 you can get the grouped document with all keys like below;

var group = new BsonDocument
{
 { "_id", "$groupField" },
 { "_document", new BsonDocument { { "$first", "$$ROOT" } } }
};

ProjectionDefinition<BsonDocument> projection = new BsonDocument{{ "document", "$_document"}};
var result = await col.Aggregate().Group(group).Project(projection).ToListAsync();
    
// For demo first record 
var fistItemAsT = BsonSerializer.Deserialize<T>(result.ToArray()[0]["document"].AsBsonDocument);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsamuelluisView Question on Stackoverflow
Solution 1 - MongodbstyvaneView Answer on Stackoverflow
Solution 2 - MongodbDekaView Answer on Stackoverflow
Solution 3 - MongodbVictoria MalayaView Answer on Stackoverflow
Solution 4 - MongodbDeeksha SharmaView Answer on Stackoverflow
Solution 5 - MongodbGhopper21View Answer on Stackoverflow
Solution 6 - Mongodbmr.byteView Answer on Stackoverflow