What is wrong with this MongoDB Aggregate? - mongodb

This is my sample document.I have many such documents where Cpe.Srno is a key and Cpe.date keeps getting updated using an Api
{
"_id": "8NH5rNCz47fyoo3z2",
"Cpe": {
"Srno": "GZDBDt2NmpMv54j",
"date": {
"$date": "2016-12-04T14:51:26.452Z"
}
},
"serialNumber": 4.703961341e+09,
"Device": {
"object_id": 1,
"terminal_id": 3,
"Info": {
"FirmwareVersion": {
"value": "5.9.2"
},
"Model": {
"value": "lunus Rustic HRK paxton - 989"
},
"DeviceSerialNumber": {
"value": 3.830919496e+09
},
"BatteryType": {
"value": "Handcrafted lithium battery"
},
"SavedPriority": {
"value": 7
}
}
}
}
I am trying to group by Cpe.Srno and get the latest document using Cpe.date.So i get distinct Cpe.Srno and only the latest documents.
I am stuck at the group stage.This is my code
db.AllDevices.aggregate([
{ $sort: { "Cpe.date": -1 } },
{
$group: {
_id: "$Cpe.Srno",
"latest": { $first: "$Cpe.date" }
}
},
])
and this is my result
{ "_id" : "qst3pnS3EQi8uHb", "latest" : ISODate("2016-12-04T14:51:26.487Z") }
{ "_id" : "Ur45SS1I3Yaji2p", "latest" : ISODate("2016-12-04T14:51:26.513Z") }
{ "_id" : "9ZZXVVEAQ5pA9Ax", "latest" : ISODate("2016-12-04T14:51:26.518Z") }
I need to get the whole data,all the fields(I only get two fields).I checked out $push ,$addToSet.They don't seem to work for me.
Where am I going wrong or am I missing something.
Tried $match after $group
db.AllDevices.aggregate([
{ $sort: { "Cpe.date": -1 } },
{
$group: {
_id: "$Cpe.Srno",
"latest": { $first: "$Cpe.date" }
}
},
{
$project: {
Srno: "$_id",
datetag: "$latest",
_id: 0
}
},
{ $match: { "Cpe.Srno": "$Srno", "Cpe.date": "$datetag" } }
])
Doesnt work.

You can make use of $$ROOT to push the whole document followed by project with $arrayElemAt to pick the latest.
aggregate([{
$sort: {
"Cpe.date": -1
}
}, {
$group: {
_id: "$Cpe.Srno",
"fields": {
$push: "$$ROOT"
}
}
}, {
$project: {
"latest": {
$arrayElemAt: ["$fields", 0]
}
}
}])

Related

MongoDB group with project

I am trying to select maximum value of a field along with the time it occured.
db.temperaterdata.aggregate([
{
$match: {
$and: [
{ "organization_id": 37 },
{
"resulttime":{
$gte:'2020-05-01 00:00',
$lte:'2020-05-29 23:59'
}
}
]
}
},
{
"$group": {
"_id": "$userid",
maxTemperature: { $max: "$temperature"},
minTemperature: { $min: "$temperature"}
}
},
{
"$project": {
"resulttime":1,
maxTemperature:1,
minTemperature:1
}
}
]);
However, the time for which the maximum/minimum temperature has occurred it's not showing up. The result coming is like this:
{ "_id" : 233, "maxTemperature" : 100.67, "minTemperature" : 98.56 }
{ "_id" : 256, "maxTemperature" : 100.67, "minTemperature" : 98.56 }
{ "_id" : 263, "maxTemperature" : 100.67, "minTemperature" : 98.56 }
Any help will be much appreciated.
You need to order by userId + temperature and take max / min documents with $first / $last operators.
Try the query below:
db.temperaterdata.aggregate([
{
"$match": {
"organization_id": 37,
"resulttime": {
"$gte": "2020-05-01 00:00",
"$lte": "2020-05-29 23:59"
}
}
},
{
"$sort": {
"userid": 1,
"temperature": 1
}
},
{
"$group": {
"_id": "$userid",
"maxTemperature": {
"$last": "$$ROOT"
},
"minTemperature": {
"$first": "$$ROOT"
}
}
},
{
"$project": {
"maxresulttime": "$maxTemperature.resulttime",
"minresulttime": "$minTemperature.resulttime",
"maxTemperature": "$maxTemperature.temperature",
"minTemperature": "$minTemperature.temperature"
}
}
])
MongoPlayground

mongoose aggregate sort one array by field

I would like to fetch only one element from each group of fields:
here is my result
[{
"_id":{"device_id":"9"},
"x":["2019-11-17T18:03:33.000Z","2019-11-17T12:02:35.000Z"],
"y":["93","3"]
},
{
"_id":{"device_id":"8"},
"x":["2019-11-16T12:05:33.000Z","2019-11-16T12:02:35.000Z"],
"y":["33","3"]
}]
I would like to get only the last inserted data, means only one value from x and one value from y.
This is the code :
aggregate([
{ "$sort": { "timeStamp": -1 } },
{
$group:
{
_id: { device_id: '$device_id' },
x: {
$push: "$timeStamp"
},
y: {
$push: "$value"
},
device_name: {
$push: "$device_name"
},
}
}
}]
I have tried to add project exp:
{
$project: {
time: "$x",
value:"$y",
integerValues:{
$map:{
input: "x",
as: "integerValue",
in: { $trunc: "$$integerValue" }
}
}
}
}
This is the result I expect to have it:
{
"_id":{"device_id":"8"},
"x":["2019-11-16T12:05:33.000Z"],
"y":["33"]
},
{
"_id":{"device_id":"9"},
"x":["2019-11-17T18:03:33.000Z"],
"y":["93"]
}
I was searching so much about this problem without any solution
All you need is a single project stage
aggregate([{
$project: {
x: {
$max: "$x"
},
y: {
$max: "$y"
}
}
}])
The result you get will be something like this,
[
{
"_id": {
"device_id": "9"
},
"x": "2019-11-17T18:03:33.000Z",
"y": "93"
},
{
"_id": {
"device_id": "8"
},
"x": "2019-11-16T12:05:33.000Z",
"y": "33"
}
]

I can nt make a sum in a nested mongo query

I tried all weekend to write this query, but it is not working. The nested field worked, but the other field does not show the $sum.
This is a example of the collections:
{
"_id": ObjectId("5dc28a3fd89a4d7bb82a75b4"),
"bedrijf": ObjectId("5c7ee51d2478a30fa4357b4c"),
"doelgroep": "Kinderen 12-",
"recreatieleider": 1,
"__v": 0
}
I like to group the key "bedrijf" and make a subgroup "doelgroep" The code I use is:
{
$group: {
_id: {
bedrijf: "$bedrijf",
doelgroep: "$doelgroep",
},
"totaal": {
$sum: 1
}
}
},
{
$group : {
_id : '$_id.bedrijf',
doelgroep : {
"$push": {
doelgroep:"$_id.doelgroep",
total:"$totaal"
}
},
"recreatieleider": {
$sum: "$recreatieleider"
}
}
}
But when I see the results the key "recreatieleder" does not make the sum of the entries.
{
"results": [
{
"_id": "5c69c5d939fbb38a1fcf3146",
"doelgroep": [
{
"doelgroep": "Gezinnen",
"total": 1
},
{
"doelgroep": "Kinderen 9-11",
"total": 9
}
],
"recreatieleider": 0
}
]
}
So how can I count the "recreatieleider" value?
My final expected output must be:
{
"results": [
{
"_id": "5c69c5d939fbb38a1fcf3146",
"doelgroep": [
{
"doelgroep": "Gezinnen",
"total": 1
},
{
"doelgroep": "Kinderen 9-11",
"total": 9
}
],
"recreatieleider": 20
}
]
}
added question
I ran to a other problem. You make me fixed the first problem. When a mak a other sub group it will not show me
I use this code
{
$group: {
_id: {
bedrijf: "$bedrijf",
doelgroep: "$doelgroep",
},
"totaal": {
$sum: 1
},
"hulpkrachten": {
$sum: "$hulpkrachten"
},
"recreatieleider": {
$sum: "$recreatieleider"
},
"stagiaires": {
$sum: "$stagiaires"
},
}
},
{
$group : {
_id : '$_id.bedrijf',
doelgroep : {
"$push": {
doelgroep:"$_id.doelgroep",
total:"$totaal"
}
},
soortactiviteit : {
"$push": {
soortactiviteit:"$_id.soortactiviteit",
total:"$totaal"
}
}
}
}
You can see I add "soortactiviteit" to it, but it wil not add u subgroup
The expeditor outcome must be
{
"results": [
{
"_id": "5c69c5d939fbb38a1fcf3146",
"doelgroep": [
{
"doelgroep": "Gezinnen",
"total": 1
},
{
"doelgroep": "Kinderen 9-11",
"total": 9
}
],
"soortactivieit": [
{
"doelgroep": "creativiteit",
"total": 20
},
{
"doelgroep": "sports,
"total": 9
}
],
"recreatieleider": 20
}
]
}
Maybe you can help with my last question of the subject.. Thank
Not sure, what you are trying to achieve with this query. However, below query works for your scenario.
{
$group:{
_id:{
bedrijf:"$bedrijf",
doelgroep:"$doelgroep",
},
"totaal":{
$sum:1
},
"recreatieleider":{
$sum:"$recreatieleider"
}
}
},
{
$group:{
_id:'$_id.bedrijf',
doelgroep:{
"$push":{
doelgroep:"$_id.doelgroep",
total:"$totaal"
}
},
"recreatieleider":{
$sum:"$recreatieleider"
}
}
}
When first $group runs the key recreatieleider is lost in
{
$group: {
_id: {
bedrijf: "$bedrijf",
doelgroep: "$doelgroep",
},
"totaal": {
$sum: 1
}
}
},
change it to and try then:
{
$group: {
_id: {
bedrijf: "$bedrijf",
doelgroep: "$doelgroep",
},
"totaal": {
$sum: 1
},
"recreatieleider": {
$sum: "$recreatieleider"
}
}
},

MongoDB get values with most recent date

I have a collection with documents such as these.
What I want to do is get all distinct clusters coming from the document with the highest (recent) lastupdate field.
I think this should be the output:
[
"19":"Income2",
"20":"Income Modified",
"21":"Income Modified"
]
Please try this :
db.yourCollection.aggregate([{ $unwind: '$meta.clusters' },
{ $project: { '_id': { $objectToArray: '$meta.clusters' }, 'last_update': 1 } }, { $sort: { 'last_update': -1 } },
{ $group: { _id: '$_id.k', values: { $first: '$$ROOT' } } }, { $sort: { 'values.last_update': -1 } },
{ $replaceRoot: { 'newRoot': '$values' } },
{ $group: { _id: '', distinctCLusters: { $push: { $arrayToObject: "$_id" } } } }, { $project: { _id: 0 } }])
Output with provided data:
{
"distinctCLusters" : [
{
"21" : "Income Modified"
},
{
"19" : "Income2"
},
{
"20" : "Income Modified"
}
]
}

How to get top level elements as well as one level down array elements aggregate in one mongo query?

I have 2 mongo aggregate queries that work well separately -
db.transfer_orders.aggregate([
{
$match: {
"request_timestamp": { $gte: ISODate("2017-10-00T00:00:00.000Z") },
"request_timestamp": { $lt: ISODate("2017-10-12T00:00:00.000Z") },
"purpose": "POSITIONING"
}
},
{
$group: {
_id: null,
to_count: { $sum: 1 },
qty: { $sum: "$quantity" }
}
},
{
$project: {
_id: 0,
"to_count": "$to_count",
"qty": "$qty"
}
}
])
and
db.transfer_orders.aggregate([
{
$match: {
"request_timestamp": { $gte: ISODate("2017-10-00T00:00:00.000Z") },
"request_timestamp": { $lt: ISODate("2017-10-12T00:00:00.000Z") },
"purpose": "POSITIONING"
}
},
{
$unwind: "$adjustments"
},
{
$group: {
_id: null,
totalChangeQty: { $sum: "$adjustments.change_in_quantity"}
}
},
{
$project: {
_id: 0,
"adjusted_quantity": "$totalChangeQty"
}
}
])
The first query returns aggregate data of elements at the top level of the document, { "to_count" : 7810, "qty" : 19470 }
The second query returns aggregate data of elements at one level below the top level for the "adjustments" array - { "adjusted_quantity" : -960 }
Is there a way to write this as one query that will return both sets of data since the match criteria is the same for both?
The following aggregate operation should suffice since it has a pipeline after the $match step that introduces the new field adjusted_quantity. This is made possible using the $sum which returns the sum of the specified list of expressions for each document.
Once it reaches the $group stage, you can retain the value using the $sum operator.
db.transfer_orders.aggregate([
{
"$match": {
"request_timestamp": { "$gte": ISODate("2017-10-00T00:00:00.000Z") },
"request_timestamp": { "$lt": ISODate("2017-10-12T00:00:00.000Z") },
"purpose": "POSITIONING"
}
},
{
"$addFields": {
"adjusted_quantity": {
"$sum": "$adjustments.change_in_quantity"
}
}
},
{
"$group": {
"_id": null,
"to_count": { "$sum": 1 },
"qty": { "$sum": "$quantity" },
"adjusted_quantity": { "$sum": "$adjusted_quantity" }
}
}
])