Preserve $group collapsed fields


this is not me

I want to summarize a set of documents by relying on a field called code. How can I aggregate the data and retain the details in the original document?

The pipeline input contains the following documents.

{ 
    "_id" : ObjectId("5ff38e0eb09dec2cbce14760"), 
    "code" : "U", 
    "date" : ISODate("2021-04-09T00:00:00.000+0000"), 
    "full_day" : false, 
    "remote" : false, 
    "student_id" : 9441
}
{ 
    "_id" : ObjectId("5ff38e0eb09dec2cbce14807"), 
    "code" : "E", 
    "date" : ISODate("2020-11-02T00:00:00.000+0000"), 
    "full_day" : false, 
    "remote" : false, 
    "student_id" : 9441
}
{ 
    "_id" : ObjectId("5ff39854b09dec2cbce1494c"), 
    "code" : "E", 
    "date" : ISODate("2020-11-03T08:00:00.000+0000"), 
    "full_day" : true, 
    "remote" : false, 
    "student_id" : 9441
}

The desired output is grouped by code, promoted student_idto the root level, and nested with additional details in detailsan array:

{ 
    "code" : "U",
    "student_id": 9441, 
    "count" : 1.0, 
    "details" : [
        {
            "date" : ISODate("2021-04-09T00:00:00.000+0000"), 
            "full_day" : false, 
            "remote" : false, 
        }
    ]
}
{ 
    "code" : "E",
    "student_id": 9441, 
    "count" : 2.0, 
    "details" : [
        {
            "date" : ISODate("2020-11-02T00:00:00.000+0000"), 
            "full_day" : false, 
            "remote" : false, 
        }, 
        {
            "date" : ISODate("2020-11-03T08:00:00.000+0000"), 
            "full_day" : true, 
            "remote" : false, 
        }
    ]
}

Combined $group, $pushI can only produce:

{ 
    "_id" : "U", 
    "count" : 1.0, 
    "details" : [
        {
            "date" : ISODate("2021-04-09T00:00:00.000+0000"), 
            "full_day" : false, 
            "remote" : false, 
            "student_id" : 9441
        }
    ]
}
{ 
    "_id" : "E", 
    "count" : 2.0, 
    "details" : [
        {
            "date" : ISODate("2020-11-02T00:00:00.000+0000"), 
            "full_day" : false, 
            "remote" : false, 
            "student_id" : 9441
        }, 
        {
            "date" : ISODate("2020-11-03T08:00:00.000+0000"), 
            "full_day" : true, 
            "remote" : false, 
            "student_id" : 9441.0
        }
    ]
}

The above result is achieved with the following pipeline:

    [
        { 
            "$match" : { 
                "student_id" : 9441.0
            }
        }, 
        { 
            "$group" : { 
                "_id" : "$code", 
                "count" : { 
                    "$sum" : 1.0
                }, 
                "details" : { 
                    "$push" : { 
                        "date" : "$date", 
                        "full_day" : "$full_day", 
                        "remote" : "$remote", 
                        "student_id" : "$student_id"
                    }
                }
            }
        }, 
        { 
            "$addFields" : { 
                "student_id" : "$student_id"
            }
        }
    ]
Joe

If you want a field to have the same value for all input documents, and you want that field to be included in the $groupoutput, use the $first accumulation operator:

 { 
            "$group" : { 
                "_id" : "$code", 
                "student_id" : {$first: "$student_id"},
                "count" : { 
                    "$sum" : 1.0
                }, 
                "details" : { 
                    "$push" : { 
                        "date" : "$date", 
                        "full_day" : "$full_day", 
                        "remote" : "$remote"
                    }
                }
            }
        }

If you need to rename _idto code, use a stage $projectafter the group .

Related


Preserve $group collapsed fields

this is not me I want to summarize a set of documents by relying on a field called code. How can I aggregate the data and retain the details in the original document? The pipeline input contains the following documents. { "_id" : ObjectId("5ff38e0eb09dec2

Preserve $group collapsed fields

this is not me I want to summarize a set of documents by relying on a field called code. How can I aggregate the data and retain the details in the original document? The pipeline input contains the following documents. { "_id" : ObjectId("5ff38e0eb09dec2

Preserve $group collapsed fields

this is not me I want to summarize a set of documents by relying on a field called code. How can I aggregate the data and retain the details in the original document? The pipeline input contains the following documents. { "_id" : ObjectId("5ff38e0eb09dec2

Preserve $group collapsed fields

this is not me I want to summarize a set of documents by relying on a field called code. How can I aggregate the data and retain the details in the original document? The pipeline input contains the following documents. { "_id" : ObjectId("5ff38e0eb09dec2

mongo group query how to preserve fields

plus Everyone In a mongo group query, the result shows only the key in the parameter. How to keep the first document in each group like mysql query group. E.g: ------------------------------------------------------------------------- | name | age | sex |

mongo group query how to preserve fields

plus Everyone In a mongo group query, the result shows only the key in the parameter. How to keep the first document in each group like mysql query group. E.g: ------------------------------------------------------------------------- | name | age | sex |

mongo group query how to preserve fields

plus Everyone In a mongo group query, the result shows only the key in the parameter. How to keep the first document in each group like mysql query group. E.g: ------------------------------------------------------------------------- | name | age | sex |

How to preserve expanded/collapsed state in jqgrid grouping?

Viru Narasimman I have implemented jqgrid in grouping method. By default I use groupCollapse:truejqgrid's parameters to make the groups collapsed . My grid works fine, but when I expand the group and sort the column, the whole grid reloads and the expanded sta

How to preserve expanded/collapsed state in jqgrid grouping?

Viru Narasimman I have implemented jqgrid in grouping method. By default I use groupCollapse:truejqgrid's parameters to make the groups collapsed . My grid works fine, but when I expand the group and sort the column, the whole grid reloads and the expanded sta

How to preserve expanded/collapsed state in jqgrid grouping?

Viru Narasimman I have implemented jqgrid in grouping method. By default I use groupCollapse:truejqgrid's parameters to make the groups collapsed . My grid works fine, but when I expand the group and sort the column, the whole grid reloads and the expanded sta

How to preserve expanded/collapsed state in jqgrid grouping?

Viru Narasimman I have implemented jqgrid in grouping method. By default I use groupCollapse:truejqgrid's parameters to make the groups collapsed . My grid works fine, but when I expand the group and sort the column, the whole grid reloads and the expanded sta

How to preserve expanded/collapsed state in jqgrid grouping?

Viru Narasimman I have implemented jqgrid in grouping method. By default I use groupCollapse:truejqgrid's parameters to make the groups collapsed . My grid works fine, but when I expand the group and sort the column, the whole grid reloads and the expanded sta

How to preserve expanded/collapsed state in jqgrid grouping?

Viru Narasimman I have implemented jqgrid in grouping method. By default I use groupCollapse:truejqgrid's parameters to make the groups collapsed . My grid works fine, but when I expand the group and sort the column, the whole grid reloads and the expanded sta

How to preserve expanded/collapsed state in jqgrid grouping?

Viru Narasimman I have implemented jqgrid in grouping method. By default I use groupCollapse:truejqgrid's parameters to make the groups collapsed . My grid works fine, but when I expand the group and sort the column, the whole grid reloads and the expanded sta

Determines the collapsed group of a list view

Tom L. Is there any way to determine (if possible, programmatically set) which groups are collapsed and which are not in the listview. Here's how to set up a list view grouping: CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(LvMslInf

How to preserve input state of ngb accordion when collapsed

Tilias Is it possible to save the state of the input (entered text) when collapsing an ngb accordion? For example : https://stackblitz.com/edit/angular-ukshlz-wz6st8?file=app/accordion-basic.html If you enter some text in the input and collapse/expand the firs

How to preserve input state of ngb accordion when collapsed

Tilias Is it possible to save the state of the input (entered text) when collapsing an ngb accordion? For example : https://stackblitz.com/edit/angular-ukshlz-wz6st8?file=app/accordion-basic.html If you enter some text in the input and collapse/expand the firs

How to preserve input state of ngb accordion when collapsed

Tilias Is it possible to save the state of the input (entered text) when collapsing an ngb accordion? For example : https://stackblitz.com/edit/angular-ukshlz-wz6st8?file=app/accordion-basic.html If you enter some text in the input and collapse/expand the firs

How to preserve input state of ngb accordion when collapsed

Tilias Is it possible to save the state of the input (entered text) when collapsing an ngb accordion? For example : https://stackblitz.com/edit/angular-ukshlz-wz6st8?file=app/accordion-basic.html If you enter some text in the input and collapse/expand the firs

How to preserve input state of ngb accordion when collapsed

Tilias Is it possible to save the state of the input (entered text) when collapsing an ngb accordion? For example : https://stackblitz.com/edit/angular-ukshlz-wz6st8?file=app/accordion-basic.html If you enter some text in the input and collapse/expand the firs

How to preserve input state of ngb accordion when collapsed

Tilias Is it possible to save the state of the input (entered text) when collapsing an ngb accordion? For example : https://stackblitz.com/edit/angular-ukshlz-wz6st8?file=app/accordion-basic.html If you enter some text in the input and collapse/expand the firs

Preserve constructor when annotating fields

Nason: I have the following class. public class StatusCategory { @JsonProperty("key") private final String m_key = null; public String getKey() { return(m_key); } } What -keepoptions are there to ensure that Proguard doesn't delete the c

Preserve input fields in Python Tkinter

Tomlinson I have a text input field in my GUI that asks for two entries #1 Filepath #2 Item (these reference an excel sheet) File paths rarely change, but still need the ability to change when needed Is it possible to keep the file path in the input field so i

Preserve blank fields in model with Angular

Mathias Ronnlund If I have an angular form like this <form name="myForm"> <input type="text" required ng-model="field1" /> <input type="text" ng-model="field2" /> </form> When I access the model, it will keep only the fields with values, so if both fields

Preserve input fields in Python Tkinter

Tomlinson I have a text input field in my GUI that asks for two entries #1 Filepath #2 Item (these reference an excel sheet) File paths rarely change, but still need the ability to change when needed Is it possible to keep the file path in the input field so i