How to create nested array inside $group in MongoDB? - mongodb

After some of the aggregation pipeline stages, my MongoDB Document has the following format:
{
"key": "A",
"status": "To Do",
"val": 4
},
{
"key": "A",
"status": "Done",
"val": 1
},
{
"key": "A",
"status": "To Do",
"val": 3
},
{
"key": "A",
"status": "Done",
"val": 2
},
{
"key": "B",
"status": "Done",
"val": 5
},
{
"key": "B",
"status": "Done",
"val": 6
}
Expected output:
Group by key
Group by status
Push val
{
"key": "A",
"status_val": [
{
"status": "To Do",
"val": [
3,
4
]
},
{
"status": "Done",
"val": [
1,
2
]
}
]
},
{
"key": "B",
"status_val": "Done",
"val": [
5,
6
]
}
Here is what I tried:
db.collection.aggregate([
{
$group: {
_id: {
key:"$key",
status:"$status"
},
v: {
$push: "$val"
}
}
},
{
$group: {
_id: "$_id.key",
status_val: {
$addToSet: {
status: "$_id.status",
val: "$_id.v"
}
}
}},
{
$project: { _id: 0,key:"$_id",status_val:1 }
}
]).pretty()
But I get the wrong output:
[
{
"key": "B",
"status_val": [
{
"status": "Done"
}
]
},
{
"key": "A",
"status_val": [
{
"status": "Done"
},
{
"status": "To Do"
}
]
}
]
How to get nested array inside $group?

You're correctly running $group twice but this line needs to be changed:
val: "$_id.v"
v is not a part of _id, your data after first $group looks like this:
{
"_id": { "key": "B, "status": "Done" },
"v": [ 5, 6 ]
}
try:
db.collection.aggregate([
{
$group: {
_id: { key: "$key", status: "$status" },
val: { $push: "$val" }
}
},
{
$group: {
_id: "$_id.key",
status_val: {
$push: {
status: "$_id.status",
val: "$val"
}
}
}
},
{
$project: {
_id: 0,
key: "$_id",
status_val: 1
}
}
])
Mongo Playground

Related

mongoDB $concat string with array item

This is my collection:
[
{
"_id": {
"$oid": "6332dda58121948311cbdb67"
},
"date": "2022-09-13",
"file": "xxx",
"package": 1,
"userList": [
{
"userName": "user_1",
"crDate": "2022.09.28",
"boolId": 1
}
]
},
{
"_id": {
"$oid": "6332dda58121948311cbdb68"
},
"date": "2022-09-13",
"file": "xxx",
"package": 2,
"userList": []
}
]
My desired output would be this (if all of the userList array is empty):
[
{
"_id": "2022-09-13",
"items": [
{
"fileName": "xxx",
"items": [
{
"package": "1 - na"
},
{
"package": "2 - na"
}
]
}
]
}
]
And this if I would have an object inside the userList array:
[
{
"_id": "2022-09-13",
"items": [
{
"fileName": "xxx",
"items": [
{
"package": "1 - user_1"
},
{
"package": "2 - na"
}
]
}
]
}
]
I try to run this aggregate:
db.collection.aggregate([
{
$group: {
_id: {
"date": "$date",
"file": "$file",
},
"items": {
$push: {
"package": {
$concat: [
{
$toString: "package"
},
" - ",
{
$toString: {
$arrayElemAt: [
"$userList",
0
]
}
}
]
}
}
}
}
},
{
$group: {
_id: "$_id.date",
"items": {
$push: {
"fileName": "$_id.file",
"items": "$items"
}
}
},
},
])
It's running if the userList array is empty, but it's not returning the desired output and if the userList array is not empty it throws this error:
Mongo Server error (MongoCommandException): Command failed with error 241 (ConversionFailure): 'Unsupported conversion from object to string in $convert with no onError value' on server
Here comes an example: mongo_playground
How should I modify the aggregate?
It requires 2 fixes in the first $group stage,
missed the $ sign in package property name, in { $toString: "$package" }
use property name userName while accessing first element in $userList.userName
use $ifNull operator to check if the property is not present then it returns "na" string
db.collection.aggregate([
{
$group: {
_id: {
"date": "$date",
"file": "$file"
},
"items": {
$push: {
"package": {
$concat: [
{ $toString: "$package" }, // <====== here
" - ",
{
$toString: {
$ifNull: [
{ $arrayElemAt: ["$userList.userName", 0] }, // <====== here
"na"
]
}
}
]
}
}
}
}
},
{
$group: {
_id: "$_id.date",
"items": {
$push: {
"fileName": "$_id.file",
"items": "$items"
}
}
}
}
])
Playground

Group Subdocuments Array by key in MongoDB

Is it possible to use MongoDB's aggregation framework to group channels using folder key and without joining documents?
[{key: 1, channels: {A: [], B: [], etc}}, {key: 2, channels: {A: [], B: [], etc}}]
I am trying to do using $unwind and then $group by folder name but it seems impossible.
Mongo Playground
Documents:
[
{
key: 1,
channels: [
{
"id": 1,
"name": "XXX",
"folder": "C"
},
{
"id": 2,
"name": "XXX",
"folder": "A"
},
{
"id": 3,
"name": "XXX",
"folder": "B"
},
{
"id": 4,
"name": "XXX",
"folder": "A"
},
{
"id": 5,
"name": "XXX",
"folder": "B"
},
{
"id": 6,
"name": "XXX",
"folder": "C"
}
]
},
{
key: 2,
channels: [
{
"id": 1,
"name": "XXX",
"folder": "D"
},
{
"id": 2,
"name": "XXX",
"folder": "B"
},
{
"id": 3,
"name": "XXX",
"folder": "A"
},
{
"id": 4,
"name": "XXX",
"folder": "C"
},
{
"id": 5,
"name": "XXX",
"folder": "A"
},
{
"id": 6,
"name": "XXX",
"folder": "D"
}
]
}
]
Expected Result:
[
{
key: 1,
channels: {
A: [{
"id": 2,
"name": "XXX"
},{
"id": 4,
"name": "XXX"
}],
B: [{
"id": 3,
"name": "XXX"
}, {
"id": 5,
"name": "XXX"
}],
C: [{
"id": 1,
"name": "XXX"
},{
"id": 6,
"name": "XXX"
}]
}
},
{
"key": 2,
"channels": ...
}
]
Thank you very much in advance.
$group by key and folder, construct channels array by providing required fields
$group by only key and construct the channels array in key-vlaue format
$arrayToObject convert above constructed channels to object
db.collection.aggregate([
{ $match: { key: { $in: [1, 2] } } },
{ $unwind: "$channels" },
{
$group: {
_id: {
id: "$key",
folder: "$channels.folder"
},
channels: {
$push: {
id: "$channels.id",
name: "$channels.name"
}
}
}
},
{
$group: {
_id: "$_id.id",
channels: {
$push: {
k: "$_id.folder",
v: "$channels"
}
}
}
},
{ $project: { channels: { $arrayToObject: "$channels" } } }
])
Playground
You just have to add one more $group stage followed by project stage which will alter the data as per requirement.
db.collection.aggregate([
{
$match: {
key: {
$in: [
1,
2
]
}
}
},
{
$unwind: "$channels"
},
{
$group: {
_id: {
key: "$key",
folder: "$channels.folder"
},
value: {
$push: "$channels",
},
"foldersRef": {
$push: "$channels.folder"
},
}
},
{
$group: {
_id: "$_id.key",
"channels": {
"$push": "$value"
},
},
},
{
"$project": {
"channels": {
"$map": {
"input": "$channels",
"as": "channel",
"in": {
"$arrayToObject": [
[
{
"v": {
"$map": {
"input": "$$channel",
"as": "subChannel",
"in": {
"id": "$$subChannel.id",
"name": "$$subChannel.name",
}
}
},
"k": {
"$arrayElemAt": [
"$$channel.folder",
0
]
},
}
]
],
},
}
},
},
},
])
Mongo Playground Sample Execution
What you have done is more appreciated. You might need two grop to easly preform the aggregations
$unwind to deconstruct the array
$group to group by key and foldername and second group to group by key only, but make the grp as key value pair which helps for $arrayToObject
$replaceRoot to make it to root with other fields _id ($mergeobjects)
$project for projection
Here is the code
db.collection.aggregate([
{ $unwind: "$channels" },
{
"$group": {
"_id": { key: "$key", fname: "$channels.folder" },
"channels": { "$push": "$channels" }
}
},
{
"$group": {
"_id": "$_id.key",
"grp": { "$push": { k: "$_id.fname", v: "$channels" } }
}
},
{ "$addFields": { "grp": { "$arrayToObject": "$grp" } } },
{
"$replaceRoot": {
"newRoot": { "$mergeObjects": [ "$grp", "$$ROOT" ] }
}
},
{
"$project": { grp: 0 }
}
])
Working Mongo playground

Adding Group counters and Items counter in MongoDB?

I have this Json :
[
{
"key": "guitar",
"name": "john"
},
{
"key": "guitar",
"name": "paul"
},
{
"key": "guitar",
"name": "george"
},
{
"key": "drums",
"name": "ringo"
}
]
Let's group by key and add items to each group :
db.collection.aggregate([
{
$sort: {
name: -1
}
},
{
$group: {
_id: "$key",
"projects": {
"$push": "$$ROOT"
},
count: {
$sum: 1
}
}
},
{
$sort: {
"_id": 1
}
}
])
Result:
[
{
"_id": "drums",
"count": 1,
"projects": [
{
"_id": ObjectId("5a934e000102030405000003"),
"key": "drums",
"name": "ringo"
}
]
},
{
"_id": "guitar",
"count": 3,
"projects": [
{
"_id": ObjectId("5a934e000102030405000001"),
"key": "guitar",
"name": "paul"
},
{
"_id": ObjectId("5a934e000102030405000000"),
"key": "guitar",
"name": "john"
},
{
"_id": ObjectId("5a934e000102030405000002"),
"key": "guitar",
"name": "george"
}
]
}
]
Question:
How can I add counter such as this :
[
{
"_id": "drums",
"Counter":0, // <--------------------------------
"count": 1,
"projects": [
{
"_id": ObjectId("5a934e000102030405000003"),
"key": "drums",
"name": "ringo",
"InnerCounter":0 // <--------------------------------
}
]
},
{
"_id": "guitar",
"Counter":1, // <--------------------------------
"count": 3,
"projects": [
{
"_id": ObjectId("5a934e000102030405000001"),
"key": "guitar",
"name": "paul",
"InnerCounter":0 // <--------------------------------
},
{
"_id": ObjectId("5a934e000102030405000000"),
"key": "guitar",
"name": "john",
"InnerCounter":1 // <--------------------------------
},
{
"_id": ObjectId("5a934e000102030405000002"),
"key": "guitar",
"name": "george",
"InnerCounter":2 // <--------------------------------
}
]
}
]
Live Demo
I don't think is there any other option to do this, You can try using $unwind to create index field in array using includeArrayIndex property,
db.collection.aggregate([
{ $sort: { name: -1 } },
{
$group: {
_id: "$key",
projects: { $push: "$$ROOT" }
}
},
// $unwind "projects" array and store index in field "projects.InnerCounter"
{
$unwind: {
path: "$projects",
includeArrayIndex: "projects.InnerCounter"
}
},
// again group by _id and reconstruct "projects" array
{
$group: {
_id: "$_id",
projects: { $push: "$projects" },
count: { $sum: 1 }
}
},
// sort by "_id"
{ $sort: { "_id": 1 } },
// group by $$ROOT and construct root array
{
$group: {
_id: null,
root: { $push: "$$ROOT" }
}
},
// deconstruct root array and add field "Counter" ofr index counter
{
$unwind: {
path: "$root",
includeArrayIndex: "root.Counter"
}
},
// replace to root
{ $replaceRoot: { newRoot: "$root" } }
])
Playground
Another option for projects array without $unwind,
Playground

MongoDb aggregation with arrays inside an array possible

I am struggling to find some examples of using the mongo aggregation framework to process documents which has an array of items where each item also has an array of other obejects (array containing an array)
In the example document below what I would really like is an example that sums the itemValue in the results array of all cases in the document and accross the collection where the result.decision was 'accepted'and group by the document locationCode
However, even an example that found all documents where the result.decision was 'accepted' to show or that summmed the itemValue for the same would help
Many thanks
{
"_id": "333212",
"data": {
"locationCode": "UK-555-5566",
"mode": "retail",
"caseHandler": "A N Other",
"cases": [{
"caseId": "CSE525666",
"items": [{
"id": "333212-CSE525666-1",
"type": "hardware",
"subType": "print cartridge",
"targetDate": "2020-06-15",
"itemDetail": {
"description": "acme print cartridge",
"quantity": 2,
"weight": "1.5"
},
"result": {
"decision": "rejected",
"decisionDate": "2019-02-02"
},
"isPriority": true
},
{
"id": "333212-CSE525666-2",
"type": "Stationery",
"subType": "other",
"targetDate": "2020-06-15",
"itemDetail": {
"description": "staples box",
"quantity": 3,
"weight": "1.66"
},
"result": {
"decision": "accepted",
"decisionDate": "2020-03-03",
"itemValue": "23.01"
},
"isPriority": true
}
]
},
{
"caseId": "CSE885655",
"items": [{
"id": "333212-CSE885655-1",
"type": "marine goods",
"subType": "fish food",
"targetDate": "2020-06-04",
"itemDetail": {
"description": "fish bait",
"quantity": 5,
"weight": "0.65"
},
"result": {
"decision": "accepted",
"decisionDate": "2020-03-02"
},
"isPriority": false
},
{
"id": "333212-CSE885655-4",
"type": "tobacco products",
"subType": "cigarettes",
"deadlineDate": "2020-06-15",
"itemDetail": {
"description": "rolling tobbaco",
"quantity": 42,
"weight": "2.25"
},
"result": {
"decision": "accepted",
"decisionDate": "2020-02-02",
"itemValue": "48.15"
},
"isPriority": true
}
]
}
]
},
"state": "open"
}
You're probably looking for $unwind. It takes an array within a document and creates a separate document for each array member.
{ foos: [1, 2] } -> { foos: 1 }, { foos: 2}
With that you can create a flat document structure and match & group as normal.
db.collection.aggregate([
{
$unwind: "$data.cases"
},
{
$unwind: "$data.cases.items"
},
{
$match: {
"data.cases.items.result.decision": "accepted"
}
},
{
$group: {
_id: "$data.locationCode",
value: {
$sum: {
$toDecimal: "$data.cases.items.result.itemValue"
}
}
}
},
{
$project: {
_id: 0,
locationCode: "$_id",
value: "$value"
}
}
])
https://mongoplayground.net/p/Xr2WfFyPZS3
Alternative solution...
We group by data.locationCode and sum all items with this condition:
cases[*].items[*].result.decision" == "accepted"
db.collection.aggregate([
{
$group: {
_id: "$data.locationCode",
itemValue: {
$sum: {
$reduce: {
input: "$data.cases",
initialValue: 0,
in: {
$sum: {
$concatArrays: [
[ "$$value" ],
{
$map: {
input: {
$filter: {
input: "$$this.items",
as: "f",
cond: {
$eq: [ "$$f.result.decision", "accepted" ]
}
}
},
as: "item",
in: {
$toDouble: {
$ifNull: [ "$$item.result.itemValue", 0 ]
}
}
}
}
]
}
}
}
}
}
}
}
])
MongoPlayground

how to create an array on the output of a response using aggregate in Mongodb

I have in my collection a list of objects with this structure:
[
{
"country": "colombia",
"city":"medellin",
"calification": [
{
"_id": 1,
"stars": 5
},
{
"_id": 2,
"stars": 3
}
]
},
{
"country": "colombia",
"city":"manizales",
"calification": [
{
"_id": 1,
"stars": 5
},
{
"_id": 2,
"stars": 5
}
]
},
{
"country": "argentina",
"city":"buenos aires",
"calification": [
{
"_id": 1,
"stars": 5
},
]
},
{
"country": "perĂº",
"city":"cusco",
"calification": [
{
"_id": 3,
"stars": 3
},
]
}
]
I am trying to make a filter so that the output is an amount of arrays for each country. this is the example of the output i want.
avg would be result sum 'stars'/ calification.length
{
"colombia": [
{
"city": "medellin",
"avg": 4,
"calification": [
{
"_id": 1,
"stars": 5
},
{
"_id": 2,
"stars": 3
}
]
},
{
"city": "manizales",
"avg": 5,
"calification": [
{
"_id": 1,
"stars": 5
},
{
"_id": 2,
"stars": 3
}
]
}
],
"argentina": {
"city": "buenos aires",
"avg": 5,
"calification": [
{
"_id": 1,
"stars": 5
}
]
},
"peru": {
"city": "cusco",
"avg": 4,
"calification": [
{
"_id": 1,
"stars": 4
}
]
}
}
I am trying to do this:
Alcalde.aggregate([
{
$addFields: {
colombia: {
"$push": {
"$cond": [{ $eq: ["$country", "'Colombia'"] }, true, null]
}
}
}
},
{
$project: { colombia: "$colombia" }
}
]
how can i do it
We can make it more elegant.
MongoDB has $avg operator, let's use it. Also, we can use $group operator to group cities for the same country.
At the end, applying $replaceRoot + $arrayToObject** we transform into desired result.
** it's because we cannot use such expression: {"$country":"$city"}
$replaceRoot $arrayToObject
data : { { [ {
"key" : "val", --> "key" : "val", {k:"key", v: "val"}, --> "key" : "val",
"key2" : "val2" "key2" : "val2" {k:"key2", v: "val2"} "key2" : "val2"
} } ] }
Try this one:
Alcalde.aggregate([
{
$group: {
_id: "$country",
city: {
$push: {
"city": "$city",
"avg": { $avg: "$calification.stars"},
"calification": "$calification"
}
}
}
},
{
$replaceRoot: {
newRoot: {
$arrayToObject: [ [{ "k": "$_id", "v": "$city"}] ]
}
}
}
])
MongoPlayground
EDIT: Generic way to populate city inner object
$$ROOT is variable which stores root document
$mergeObjects adds / override fields to final object
Alcalde.aggregate([
{
$group: {
_id: "$country",
city: {
$push: {
$mergeObjects: [
"$$ROOT",
{
"avg": { "$avg": "$calification.stars" }
}
]
}
}
}
},
{
$project: {
"city.country": 0
}
},
{
$replaceRoot: {
newRoot: {
$arrayToObject: [
[ { "k": "$_id", "v": "$city" } ]
]
}
}
}
])
MongoPlayground