Scala: Grouping on the basis of inner list of objects - scala

I have list of objects like:
[
{
studentId: "a1",
std: "10",
isEligible:True,
completedProjectInfo: [
{
projectNumber: "c1",
projectName: "AAT",
projectCompletedPlaces: {
count:2,
city:["Mumbai", "Delhi"]
}
}
]
},
{
studentId: "a2",
std: "10",
isEligible:false,
completedProjectInfo: [
{
projectNumber: "c3",
projectName: "AAK",
projectCompletedPlaces: {
count:1,
city:["Mumbai"]
}
}
]
},
{
studentId: "a3",
std: "10",
isEligible:false,
completedProjectInfo: [
{
projectNumber: "c3",
projectName: "AAK",
projectCompletedPlaces: {
count:1,
city:["Mumbai"]
}
}
]
},
{
studentId: "a4",
std: "10",
isEligible:true,
completedProjectInfo: [
{
projectNumber: "c1",
projectName: "AAT",
projectCompletedPlaces: {
count:2,
city:["Mumbai", "Delhi"]
}
},
{
projectNumber: "c3",
projectName: "AAK",
projectCompletedPlaces: {
count:1,
city:["Mumbai"]
}
}
]
}
]
Now I want to make groups of students with the exact same info like with above data student result should be something like this:
[
{
commonInfoStudents: [a2, a3],
std: "10",
isEligible:false,
completedProjectInfo: [
{
projectNumber: "c3",
projectName: "AAK",
projectCompletedPlaces: {
count:1,
city:["Mumbai"]
}
}
]
},
{
commonInfoStudents: ["a1"],
std: "10",
isEligible:True,
completedProjectInfo: [
{
projectNumber: "c1",
projectName: "AAT",
projectCompletedPlaces: {
count:2,
city:["Mumbai", "Delhi"]
}
}
]
},
{
commonInfoStudents: ["a4"],
std: "10",
isEligible:true,
completedProjectInfo: [
{
projectNumber: "c1",
projectName: "AAT",
projectCompletedPlaces: {
count:2,
city:["Mumbai", "Delhi"]
}
},
{
projectNumber: "c3",
projectName: "AAK",
projectCompletedPlaces: {
count:1,
city:["Mumbai"]
}
}
]
}
]
I tried few things with groupBy, I can group by with std and isEligible but don't understand how to compare inner objects.
Anyone can help here?
thank you.

There is nothing special about comparing lists, you just compare them same way as all other objects.
list
.groupMap(_.copy(studentId=0))(_.studentId)
.map { case (data, ids) => data.copy(commonInfoStudents=ids) }

Related

is there good way to combine object in list dart?

Condition is that if prodCode is same, the holdingNum should be sum and then put the stockList.
In acctPrdList, there are 3 object of prodCode is KR7020150009(prodName is bbb). So holdingNum should be sum 200+479+3556 =4235.
I want to objects in list by flutter like this.
"stockList": [
{
"prodCode": "KR7005950001",
"prodName": "aaa",
"holdingNum": "1",
"totalHoldingNum": "1"
},
{
"prodCode": "KR7020150009",
"prodName": "bbb",
"holdingNum": "4235",
"totalHoldingNum": "4235"
},
{
"prodCode": "KR7279570006",
"prodName": "ccc",
"holdingNum": "3",
"totalHoldingNum": "3"
}
]
from this data.
{
"investInfo": [
{
"orgCode": "C1AACQ0000",
"accountNum": "20402786856",
"acctPrdList": [
{
"prodCode": "KR7005950001",
"prodName": "aaa",
"holdingNum": "1",
"totalHoldingNum": "1"
},
{
"prodCode": "KR7020150009",
"prodName": "bbb",
"holdingNum": "200",
"totalHoldingNum": "4235"
},
{
"prodCode": "KR7020150009",
"prodName": "bbb",
"holdingNum": "479",
"totalHoldingNum": "4235"
}
]
},
{
"orgCode": "C1AACQ0000",
"accountNum": "20101834049",
"acctPrdList": [
{
"prodCode": "KR7020150009",
"prodName": "bbb",
"holdingNum": "3556",
"totalHoldingNum": "4235"
},
{
"prodCode": "KR7279570006",
"prodName": "ccc",
"holdingNum": "3",
"totalHoldingNum": "3"
}
]
}
]
}
This is my code.
My code is works. But I wonder if there is a clean code.
List<Stock> stockList = [];
List<Stock> addList = [];
for (var element in decryptModel.investInfo!) {
if (element.acctPrdList!.isNotEmpty) {
for (var acctPrd in element.acctPrdList!) {
if (stockList.isNotEmpty) {
for (var stock in stockList) {
if (acctPrd.prodCode == stock.prodCode) {
num holdingNum = acctPrd.holdingNum! + stock.holdingNum!;
stock.setHoldingNum = holdingNum;
addList.clear();
break;
} else {
addList.add(acctPrd);
final set = {...addList};
addList = set.toList();
}
}
stockList.addAll(addList);
} else {
stockList.add(acctPrd);
}
}
}
}

MongoDB: nested $group and $push but instead of get an array get a document

I have a couple million of bulky documents with a structure similar to this one (but with way more fields):
{ "class": 3, "type": "A", "color": "r", "filed1": "something", ... }
{ "class": 3, "type": "A", "color": "r", "filed1": "_", ... }
{ "class": 3, "type": "A", "color": "r", "filed1": "45", ... }
{ "class": 3, "type": "A", "color": "g", "filed1": "yt", ... }
{ "class": 3, "type": "B", "color": "b", "filed1": "xx", ... }
{ "class": 1, "type": "A", "color": "r", "filed1": "ds", ... }
{ "class": 1, "type": "A", "color": "r", "filed1": "bb", ... }
{ "class": 1, "type": "A", "color": "r", "filed1": "go", ... }
{ "class": 1, "type": "B", "color": "b", "filed1": "aa", ... }
{ "class": 1, "type": "B", "color": "g", "filed1": "ññ", ... }
And I want to group them by class, then by type and then by color so it looks like this.
{
_id: 3,
A: {
r: [ { field1: "something", ... }, { field1: "_", ... }, { field1: "45", ... } ],
g: [ { field1: "yt", ... } ],
}
B: {
b: [ { field1: "xx", ... } ],
}
}
{
_id: 1,
A: {
r: [ { field1: "ds", ... }, { field1: "bb", ... }, { field1: "go", ... } ]
}
B: {
b: [ { field1: "aa", ... } ],
g: [ { field1: "ññ", ... } ],
}
}
So far I have tried to $group the first level and then "unpack" the array using $arrayToObject.
{
$group: {
"_id": "$class",
data: { $push: { k: "$type", v: { color: "$color", field1: "$field1", ... } } }
}
},
{
$project: {
_id: 1,
data: { $arrayToObject: "$data" }
}
}
But this results in both the undesired field "data" and truncation of the documents.
{
_id: 3,
data: {
A: { "color": "g", "filed1": "yt", ... },
B: { "color": "b", "filed1": "xx", ... }
}
}
{
_id: 1,
data: {
A: { "color": "r", "filed1": "go", ... },
B: { "color": "g", "filed1": "ññ", ... }
}
}
I am trying to avoid using the javascript function forEach because of the size of the collection (about 2 GB) and the low specs of the server, but if you think there is no other way feel free to tell me.
db.collection.aggregate([
{
$group: {
_id: {
class: "$class",
type: "$type",
color: "$color"
},
v: { $push: { filed1: "$filed1", field2: "$field2" } }
}
},
{
$group: {
_id: {
class: "$_id.class",
type: "$_id.type"
},
docs2: { $push: { k: "$_id.color", v: "$v" } }
}
},
{
$group: {
_id: "$_id.class",
docs3: { $push: { k: "$_id.type", v: { $arrayToObject: "$docs2" } } }
}
},
{
$replaceWith: { $mergeObjects: [ { _id: "$_id" }, { $arrayToObject: "$docs3" } ] }
}
])
mongoplayground

$set not setting nested values

I am trying to $set with findOneAndUpdate, but my nested values are not setting properly.
Here is a snippet of my SCHEMA:
...
courseMap: {
hasCourseMap: { type: Boolean, default: false },
parentMap: { type: Schema.Types.ObjectId, ref: "CourseMap" },
colleges: [
{
name: { type: String },
_id: { type: Schema.Types.ObjectId, ref: "College" }
}
],
title: { type: String },
ninth: {
hsCourses: [
{
courseName: { type: String, required: true },
courseNumber: { type: String, required: true },
_id: {
type: Schema.Types.ObjectId,
ref: "HighSchoolCourse",
autopopulate: true
}
}
],
collegeCourses: [
{
name: {
type: String,
required: true
},
courseNumber: { type: String, required: true },
description: { type: String },
courseCode: { type: String, required: true },
_id: {
type: Schema.Types.ObjectId,
ref: "CollegeCourse",
autopopulate: true
}
}
],
skills: []
},
tenth: {
hsCourses: [
{
courseName: { type: String, required: true },
courseNumber: { type: String, required: true },
_id: {
type: Schema.Types.ObjectId,
ref: "HighSchoolCourse",
autopopulate: true
}
}
],
collegeCourses: [
{
name: {
type: String,
required: true
},
courseNumber: { type: String, required: true },
description: { type: String },
courseCode: { type: String, required: true },
_id: {
type: Schema.Types.ObjectId,
ref: "CollegeCourse",
autopopulate: true
}
}
],
skills: []
},
eleventh: {
hsCourses: [
{
courseName: { type: String, required: true },
courseNumber: { type: String, required: true },
_id: {
type: Schema.Types.ObjectId,
ref: "HighSchoolCourse",
autopopulate: true
}
}
],
collegeCourses: [
{
name: {
type: String,
required: true
},
courseNumber: { type: String, required: true },
description: { type: String },
courseCode: { type: String, required: true },
_id: {
type: Schema.Types.ObjectId,
ref: "CollegeCourse",
autopopulate: true
}
}
],
skills: []
},
twelfth: {
hsCourses: [
{
courseName: { type: String, required: true },
courseNumber: { type: String, required: true },
_id: {
type: Schema.Types.ObjectId,
ref: "HighSchoolCourse",
autopopulate: true
}
}
],
collegeCourses: [
{
name: {
type: String,
required: true
},
courseNumber: { type: String, required: true },
description: { type: String },
courseCode: { type: String, required: true },
_id: {
type: Schema.Types.ObjectId,
ref: "CollegeCourse",
autopopulate: true
}
}
],
skills: []
},
year5: {
hsCourses: [
{
courseName: { type: String, required: true },
courseNumber: { type: String, required: true },
_id: {
type: Schema.Types.ObjectId,
ref: "HighSchoolCourse",
autopopulate: true
}
}
],
collegeCourses: [
{
name: {
type: String,
required: true
},
courseNumber: { type: String, required: true },
description: { type: String },
courseCode: { type: String, required: true },
_id: {
type: Schema.Types.ObjectId,
ref: "CollegeCourse",
autopopulate: true
}
}
],
skills: []
}
}
...
Here is my update object:
{
"$set": {
"courseMap": {
"hasCourseMap": true,
"parentMap": "5e07fb5b40b723f03935cb31",
"colleges": [
{
"_id": "5dfee8128eacd562f0338678",
"name": "Eastfield"
},
{
"_id": "5e06a566559c3013687cc0f1",
"name": "University of North Texas at Dallas"
}
],
"ninth": {
"hsCourses": [
{
"_id": "5dfee4bb008dfb2fa4e89a43",
"abbr": "MATH",
"courseNumber": "7897987",
"courseName": "GEOMETRY",
"timestamp": "2019-12-22T03:36:27.026Z",
"__v": 0
}
],
"collegeCourses": [
{
"_id": "5dfef4d75581475988759efc",
"HSEquivCourses": [
"5dfee4bb008dfb2fa4e89a43"
],
"college": "5dfee8128eacd562f0338678",
"name": "test1",
"courseNumber": "test",
"description": "tesst",
"__v": 0
}
]
},
"tenth": {
"hsCourses": [
{
"_id": "5dfee4bb008dfb2fa4e89a43",
"abbr": "MATH",
"courseNumber": "7897987",
"courseName": "GEOMETRY",
"timestamp": "2019-12-22T03:36:27.026Z",
"__v": 0
}
],
"collegeCourses": [
{
"_id": "5e07dceb63b72fe4b74473e2",
"college": "5e06a566559c3013687cc0f1",
"name": "TEST",
"courseNumber": "tesat",
"description": "",
"courseCode": "test",
"__v": 0
}
]
},
"eleventh": {
"hsCourses": [
{
"_id": "5dfee4bb008dfb2fa4e89a43",
"abbr": "MATH",
"courseNumber": "7897987",
"courseName": "GEOMETRY",
"timestamp": "2019-12-22T03:36:27.026Z",
"__v": 0
}
],
"collegeCourses": [
{
"_id": "5dfef06ebe4ae31e7c6344bb",
"HSEquivCourses": [
"5dfee4bb008dfb2fa4e89a43"
],
"college": "5dfee8128eacd562f0338678",
"name": "Psychology",
"courseNumber": "1301",
"description": "test",
"__v": 0
}
]
},
"twelfth": {
"hsCourses": [
{
"_id": "5dfee4bb008dfb2fa4e89a43",
"abbr": "MATH",
"courseNumber": "7897987",
"courseName": "GEOMETRY",
"timestamp": "2019-12-22T03:36:27.026Z",
"__v": 0
}
],
"collegeCourses": [
{
"_id": "5e07dceb63b72fe4b74473e2",
"college": "5e06a566559c3013687cc0f1",
"name": "TEST",
"courseNumber": "tesat",
"description": "",
"courseCode": "test",
"__v": 0
}
]
},
"year5": {
"hsCourses": [
{
"_id": "5dfee4bb008dfb2fa4e89a43",
"abbr": "MATH",
"courseNumber": "7897987",
"courseName": "GEOMETRY",
"timestamp": "2019-12-22T03:36:27.026Z",
"__v": 0
}
],
"collegeCourses": [
{
"_id": "5dfef06ebe4ae31e7c6344bb",
"HSEquivCourses": [
"5dfee4bb008dfb2fa4e89a43"
],
"college": "5dfee8128eacd562f0338678",
"name": "Psychology",
"courseNumber": "1301",
"description": "test",
"__v": 0
}
]
}
}
}
}
And after using StudentProfile.findOneAndUpdate({ _id: user.profile }, update)
The following is updated, which is the top level key values, but my nested values are not setting.
"courseMap":{
"hasCourseMap" : true,
"parentMap" : ObjectId("5e07fb5b40b723f03935cb31"),
"colleges" : [
{
"_id" : ObjectId("5dfee8128eacd562f0338678"),
"name" : "Eastfield"
},
{
"_id" : ObjectId("5e06a566559c3013687cc0f1"),
"name" : "University of North Texas at Dallas"
}
],
"ninth" : {
"hsCourses" : [
{
}
],
"collegeCourses" : [
{
}
]
},
"tenth" : {
"hsCourses" : [
{
}
],
"collegeCourses" : [
{
}
]
},
"eleventh" : {
"hsCourses" : [
{
}
],
"collegeCourses" : [
{
}
]
},
"twelfth" : {
"hsCourses" : [
{
}
],
"collegeCourses" : [
{
}
]
},
"year5" : {
"hsCourses" : [
{
}
],
"collegeCourses" : [
{
}
]
}
}
Here is a snippet of the document before the update occurs:
...
"courseMap" : {
"hasCourseMap" : false,
}
...
You are assuming you want to put everything in $set, which is incorrect.
You can breakdown your update statement.
Consider following example, I start with document:
{
a: 1,
c: [
"1",
"2"
],
d: [
{
d1: "3",
d2: "4"
},
{
d3: "5"
}
],
e: {
e1: [
"6",
"7"
]
},
f: [
]
}
And when I run:
db.inserter.updateOne({
},
{
$set: {
c: [
"3.3"
]
},
$push: {
f: {
d4: "5.5"
}
}
})
Outcome:
{
"_id": ObjectId("5e1de4b6afa8c2dd9dcfd79c"),
"a": 1,
"c": [
"3.3"
],
"d": [
{
"d1": "3",
"d2": "4"
},
{
"d3": "5"
}
],
"e": {
"e1": [
"6",
"7"
]
},
"f": [
{
"d4": "5.5"
}
]
}
the trick?
The update document has two parts (could be more, and you could do dot notation) $set and $push
{ $set: { c: ["3.3"] }, $push: { f: { d4: "5.5" } } }
Running your query as it is, it works with the following output:
{
"_id" : ObjectId("5e1de61dafa8c2dd9dcfd79d"),
"courseMap" : {
"hasCourseMap" : true,
"parentMap" : "5e07fb5b40b723f03935cb31",
"colleges" : [
{
"_id" : "5dfee8128eacd562f0338678",
"name" : "Eastfield"
},
{
"_id" : "5e06a566559c3013687cc0f1",
"name" : "University of North Texas at Dallas"
}
],
"ninth" : {
"hsCourses" : [
{
"_id" : "5dfee4bb008dfb2fa4e89a43",
"abbr" : "MATH",
"courseNumber" : "7897987",
"courseName" : "GEOMETRY",
"timestamp" : "2019-12-22T03:36:27.026Z",
"__v" : 0
}
],
"collegeCourses" : [
{
"_id" : "5dfef4d75581475988759efc",
"HSEquivCourses" : [
"5dfee4bb008dfb2fa4e89a43"
],
"college" : "5dfee8128eacd562f0338678",
"name" : "test1",
"courseNumber" : "test",
"description" : "tesst",
"__v" : 0
}
]
},
"tenth" : {
"hsCourses" : [
{
"_id" : "5dfee4bb008dfb2fa4e89a43",
"abbr" : "MATH",
"courseNumber" : "7897987",
"courseName" : "GEOMETRY",
"timestamp" : "2019-12-22T03:36:27.026Z",
"__v" : 0
}
],
"collegeCourses" : [
{
"_id" : "5e07dceb63b72fe4b74473e2",
"college" : "5e06a566559c3013687cc0f1",
"name" : "TEST",
"courseNumber" : "tesat",
"description" : "",
"courseCode" : "test",
"__v" : 0
}
]
},
"eleventh" : {
"hsCourses" : [
{
"_id" : "5dfee4bb008dfb2fa4e89a43",
"abbr" : "MATH",
"courseNumber" : "7897987",
"courseName" : "GEOMETRY",
"timestamp" : "2019-12-22T03:36:27.026Z",
"__v" : 0
}
],
"collegeCourses" : [
{
"_id" : "5dfef06ebe4ae31e7c6344bb",
"HSEquivCourses" : [
"5dfee4bb008dfb2fa4e89a43"
],
"college" : "5dfee8128eacd562f0338678",
"name" : "Psychology",
"courseNumber" : "1301",
"description" : "test",
"__v" : 0
}
]
},
"twelfth" : {
"hsCourses" : [
{
"_id" : "5dfee4bb008dfb2fa4e89a43",
"abbr" : "MATH",
"courseNumber" : "7897987",
"courseName" : "GEOMETRY",
"timestamp" : "2019-12-22T03:36:27.026Z",
"__v" : 0
}
],
"collegeCourses" : [
{
"_id" : "5e07dceb63b72fe4b74473e2",
"college" : "5e06a566559c3013687cc0f1",
"name" : "TEST",
"courseNumber" : "tesat",
"description" : "",
"courseCode" : "test",
"__v" : 0
}
]
},
"year5" : {
"hsCourses" : [
{
"_id" : "5dfee4bb008dfb2fa4e89a43",
"abbr" : "MATH",
"courseNumber" : "7897987",
"courseName" : "GEOMETRY",
"timestamp" : "2019-12-22T03:36:27.026Z",
"__v" : 0
}
],
"collegeCourses" : [
{
"_id" : "5dfef06ebe4ae31e7c6344bb",
"HSEquivCourses" : [
"5dfee4bb008dfb2fa4e89a43"
],
"college" : "5dfee8128eacd562f0338678",
"name" : "Psychology",
"courseNumber" : "1301",
"description" : "test",
"__v" : 0
}
]
}
}
}
The fields I was using to $set for the ninth, tenth, etc came from a mongoose document and I was directly attempting to save them. I used .lean() and my query worked.

Select subdocument of a document and exclude a property of if in mongoDB

I have the following document structure in mongoDb.
I want to be able to select only all the courses, without the teacherproperty in it. How can I do that?
I have tried the following but it doesn´t work. I would also like that when it finds the result it returns that to the console?
How can I do that?
student.findCourses = (fcallback) => {
var jCourses = {
"courses": [
{
"courseName": "Web-development"
},
{
"courseName": "Databases"
},
{
"courseName": "Databases"
}
]
}
global.db.collection('students').find(({}, { jCourses: true, _id: false, firstName: false, lastName: false, age: false, teachers: false }).toArray, (err) => {
if (err) {
var jError = { "status": "error", "message": "ERROR -> student.js -> 001" }
console.log(jError)
return fcallback(true, jError)
}
var jOk = { "status": "ok", "message": "student.js -> found -> 000" }
console.log(jOk)
return fcallback(false, jOk)
})
}
The document:
{
"firstName": "Sarah",
"lastName": "Jepsen",
"age": 27,
"courses": [
{
"courseName": "Web-development",
"teachers": [
{
"firstName": "Santiago",
"lastName": "Donoso"
}
]
},
{
"courseName": "Databases",
"teachers": [
{
"firstName": "Dany",
"lastName": "Kallas"
},
{
"firstName": "Rune",
"lastName": "Lyng"
}
]
},
{
"courseName": "Interface-Design",
"teachers": [
{
"firstName": "Roxana",
"lastName": "Stolniceanu"
}
]
}
]
}
The result should look like this:
{
"courses": [
{
"courseName": "Web-development"
},
{
"courseName": "Databases"
},
{
"courseName": "Databases"
}
]
}
You can use dot syntax.
Like this :
global.db.collection('students').find({}, { "courses.courseName": true, _id: false }).toArray((err, docs) => {
if (err) {
var jError = { "status": "error", "message": "ERROR -> student.js -> 001" }
console.log(jError)
return fcallback(true, jError)
}
var jOk = { "status": "ok", "message": "student.js -> found -> 000" }
console.log(jOk);
console.log(docs); // Will print to the console the data that you want
return fcallback(false, jOk)
})
It will print only the courseName of courses.
Example on mongo shell :
> db.users.find().pretty()
{
"_id" : ObjectId("59ff442eb40a672f2223a14f"),
"firstName" : "Sarah",
"lastName" : "Jepsen",
"age" : 27,
"courses" : [
{
"courseName" : "Web-development",
"teachers" : [
{
"firstName" : "Santiago",
"lastName" : "Donoso"
}
]
},
{
"courseName" : "Databases",
"teachers" : [
{
"firstName" : "Dany",
"lastName" : "Kallas"
},
{
"firstName" : "Rune",
"lastName" : "Lyng"
}
]
},
{
"courseName" : "Interface-Design",
"teachers" : [
{
"firstName" : "Roxana",
"lastName" : "Stolniceanu"
}
]
}
]
}
> db.users.find({}, {_id: 1, "courses.courseName": 1}).pretty()
{
"_id" : ObjectId("59ff442eb40a672f2223a14f"),
"courses" : [
{
"courseName" : "Web-development"
},
{
"courseName" : "Databases"
},
{
"courseName" : "Interface-Design"
}
]
}
> db.users.find({}, {_id: 0, "courses.courseName": 1}).pretty()
{
"courses" : [
{
"courseName" : "Web-development"
},
{
"courseName" : "Databases"
},
{
"courseName" : "Interface-Design"
}
]
}

Grouping the results of a group query in mongodb

I have the following sample data:
{
"name": "Bob",
"mi": "K",
"martialStatus": "M",
"age": 30,
"city": "Paris",
"job": "Engineer"
}
{
"name": "Chad",
"mi": "M",
"martialStatus": "W",
"age": 31,
"city": "Paris",
"job": "Doctor"
}
{
"name": "Mel",
"mi": "A",
"martialStatus": "D",
"age": 31,
"city": "London",
"job": "Doctor"
}
{
"name": "Frank",
"mi": "F",
"martialStatus": "S",
"age": 30,
"city": "London",
"job": "Engineer"
}
I am trying to write a mongo query that would return results in the following format:
"peopleCount": 4,
"jobsList": {
"job": "Doctor",
"ageList": [
{
"age": 31,
"cityList": [
{
"city": "London",
"people": [
{
"name": "Mel",
"martialStatus": "D"
}
]
},
{
"city": "Paris",
"people": [
{
"name": "Chad",
"martialStatus": "W"
}
]
},
{
"city": "Berlin",
...
...
]
}
]
}
To try on the first two level (jobsList and ageList), I am trying the below
db.colName.aggregate([
{
$group: {
_id: { job: "$job" },
jobsList: {
$push: {
age: "$age",
city: "$city",
name: "$name",
martialStatus: "$martialStatus"
}
}
}
},
{
$group: {
_id: { age: "$age" },
ageList: {
$push: {
city: "$city",
name: "$name",
martialStatus: "$martialStatus"
}
}
}
}
]);
The above however does not work although the first group/push part works... Any hints on how to get that output format/groupping?
db.colName.aggregate([
{
$group: {
_id: { job: "$job", age: "$age", city: "$city" },
people: { $push: { name: "$name", martialStatus: "$martialStatus" } }
}
},
{
$group: {
_id: { job: "$_id.job", age: "$_id.age" },
peopleCount: { $sum: { $size: "$people" } },
cityList: { $push: { city: "$_id.city", people: "$people" } },
}
},
{
$group: {
_id: { job: "$_id.job" },
peopleCount: { $sum: "$peopleCount" },
agesList: { $push: { age: "$_id.age", cityList: "$cityList" } }
}
},
{
$group: {
_id: null,
peopleCount: { $sum: "$peopleCount" },
jobsList: { $push: { job: "$_id.job", agesList: "$agesList" } }
}
},
{
$project: { _id: 0, peopleCount: 1, jobsList: 1 }
}
]);
on the provided by you collection gives me the result
{
"peopleCount" : 4,
"jobsList" :
[
{
"job" : "Engineer",
"agesList" :
[
{
"age" : 30,
"cityList" :
[
{
"city" : "London",
"people" :
[
{ "name" : "Frank", "martialStatus" : "S" }
]
},
{
"city" : "Paris",
"people" :
[
{ "name" : "Bob", "martialStatus" : "M" }
]
}
]
}
]
},
{
"job" : "Doctor",
"agesList" :
[
{
"age" : 31,
"cityList" :
[
{
"city" : "London",
"people" :
[
{ "name" : "Mel", "martialStatus" : "D" }
]
},
{
"city" : "Paris",
"people" :
[
{ "name" : "Chad", "martialStatus" : "W" }
]
}
]
}
]
}
]
}
that seems to be correct. Thought, I am not sure it's the best solution. I am new to aggregation-framework.