Order by value in timeseries mongodb - mongodb

I have a timeseries collection like this ( mongodb documentation sample)
_id: "20101010/site-1/apache_pb.gif",
metadata: {
date: ISODate("2000-10-10T00:00:00Z"),
site: "site-1",
page: "/apache_pb.gif" },
daily: 5468426,
hourly: {
"0": 227850,
"1": 210231,
"2" : 12344,
"23": 20457 },
minute: {
"0": 3612,
"1": 3241,
...
"1439": 2819 }
what is the best solution, using aggregation framework, to sort for value of hourly ? so for example I want to order from the lower to higher in hourly in order to have something like this :
{
"2": 12344,
"23" : 20457,
"1" : 21031,
"0" : 227850
}
Thanks

Hi this same problem occurred for me then that time I changed my documents structure as below
{
"_id" : "20101010/site-1/apache_pb.gif",
"metadata" : {
"date" : ISODate("2000-10-10T00:00:00Z"),
"site" : "site-1",
"page" : "/apache_pb.gif"
},
"daily" : 5468426,
"hourly" : [
{
"hour" : 0,
"value" : 227850
},
{
"hour" : 1,
"value" : 210231
},
{
"hour" : 2,
"value" : 12344
},
{
"hour" : 23,
"value" : 20457
}
],
"minute" : [
{
"min" : 0,
"value" : 3612
},
{
"min" : 1,
"value" : 3241
},
{
"min" : 1439,
"value" : 2819
}
]
}
And in your case you want to sort hourly data according to values from lower to highest first so I write following aggregation query which may be solve your problem
db.collectionName.aggregate(
{"$unwind":"$hourly"},
{"$project":{"hour":"$hourly.hour","value":"$hourly.value"}},
{"$sort":{"hour":-1}},
{"$group":{"_id":0,"hourlyData":
{"$push": {"hour":"$hour","value":"$value"}}}}).pretty()

Related

mongodb math operations on two fields in collection

Hello i have exercise to filter all countries where gdp is greater than 0.05 on one person in country. I need to take the latest year of population. Also code of the country should have at least 3 characters. My collection looks like this:
mondial.countries
{
"_id" : ObjectId("581cb5a519ec2deb4ba71c03"),
"name" : "Germany",
"code" : "GER",
"capital" : "RN-Niamey-Niamey",
"area" : 1267000,
"gdp" : 7304,
"inflation" : 1.9,
"unemployment" : null,
"independence" : ISODate("1960-08-03T00:00:00Z"),
"government" : "republic",
"population" : [
{
"year" : 1950,
"value" : 2559703
},
{
"year" : 1960,
"value" : 3337141
},
{
"year" : 1970,
"value" : 4412638
},
{
"year" : 1977,
"value" : 5102990
},
{
"year" : 1988,
"value" : 7251626
},
{
"year" : 1997,
"value" : 9113001
},
{
"year" : 2001,
"value" : 11060291
},
{
"year" : 2012,
"value" : 17138707
}
]
}
For this example I have to take the population from year 2012 a divide it by gdp a then display it if its greater than 50000. I have been trying with function in js but idk how to show fields that are greater thatn 5000 of my operation. What is the easies way to do this?
var countries = db.mondial.countries.find({
"code": {$gte: 3},
});
while(countries.hasNext()) {
gdp = countries.next()
gdpresult = countries.population / gdp.gdp
print(gdpresult)
}
I don't know if I understood correctly. more see if it helps
db.mondial.aggregate([
{
$match:{
$expr: {
$gte:['$code',3 ]
}
}
},
{
$project: {
gdpresult: {
$map: {
input: '$population',
as: 'p',
in: {
value: {
$divide: ["$$p.value", '$gdp']
},
year: '$$p.year'
}
}
}
}
}])

How to get last document of each day in MongoDB collection?

I have a model Entry to which includes details of a hospital at a particular time. The data looks like this:
{
"_id": "5ef9c7337874820008c1a026",
"date": 1593427763640,
//... some data
"hospital": {
"_id": "5ef8d06630c364000840bb6d",
"name": "City Hospital",
//... some data
},
}
I want to get the last query of each day grouped by the hospital ID. In MySQL, it can be achieved using INNER JOIN. How can I do it using MongoDB?
Given a day, calculate start and end of a day.
This is to be used for filtering records, $match
start_of_day_ephocs=
end_of_day_ephocs=
Aggregate Query
sort by date, Group by hospital id,and select first document
db.Entry.aggregate(
[
{ "$match": { "date": {"$gte":start_of_day_ephocs,"$lte":end_of_day_ephocs }} },
{ "$sort": { "date": -1 } },
{
$group:
{
"_id": "$hospital._id",
"last_document": { "$first": "$$ROOT" }
}
}
]
)
Consider a sales collection with the following documents:
{ "_id" : 1, "item" : "abc", "date" : ISODate("2014-01-01T08:00:00Z"), "price" : 10, "quantity" : 2 }
{ "_id" : 2, "item" : "jkl", "date" : ISODate("2014-02-03T09:00:00Z"), "price" : 20, "quantity" : 1 }
{ "_id" : 3, "item" : "xyz", "date" : ISODate("2014-02-03T09:05:00Z"), "price" : 5, "quantity" : 5 }
{ "_id" : 4, "item" : "abc", "date" : ISODate("2014-02-15T08:00:00Z"), "price" : 10, "quantity" : 10 }
{ "_id" : 5, "item" : "xyz", "date" : ISODate("2014-02-15T09:05:00Z"), "price" : 5, "quantity" : 10 }
{ "_id" : 6, "item" : "xyz", "date" : ISODate("2014-02-15T12:05:10Z"), "price" : 5, "quantity" : 5 }
{ "_id" : 7, "item" : "xyz", "date" : ISODate("2014-02-15T14:12:12Z"), "price" : 5, "quantity" : 10 }
The following operation first sorts the documents by item and date, and then in the following $group stage, groups the now sorted documents by the item field and uses the $last accumulator to compute the last sales date for each item:
db.sales.aggregate(
[
{ $sort: { item: 1, date: 1 } },
{
$group:
{
_id: "$item",
lastSalesDate: { $last: "$date" }
}
}
]
)
The operation returns the following results:
{ "_id" : "xyz", "lastSalesDate" : ISODate("2014-02-15T14:12:12Z") }
{ "_id" : "jkl", "lastSalesDate" : ISODate("2014-02-03T09:00:00Z") }
{ "_id" : "abc", "lastSalesDate" : ISODate("2014-02-15T08:00:00Z") }
Resource

Mongodb query to group by multiple fields and filter

I want to be able to group each "Place" to show over time, how many "PatientIds" they are seeing on a given day and then be able to filter this by what the action is.
Basically Total Patients on y-axis, Date on x-axis and then a filter or stacked chart to show the action. I also thought about a mapreduce, but have never done that in mongo
I can't figure out the correct mongo query. Right now I have:
db.collection.aggregate({"$group":{_id:{place:"$place",date:"$date",action:"$action",count:{$sum:1}}},{$sort:{"_id.date":1,"_id.place":1}})
However, this is just listing out the data. I tried to do a match on all places, but that didn't give me the results I was looking for either. Any ideas?
Example json:
{
"_id" : ObjectId(""),
"patientId" : "100",
"place" : "1",
"action" : "DIAGNOSED",
"date" : ISODate("2017-01-20")
}
{
"_id" : ObjectId(""),
"patientId" : "101",
"place" : "1",
"action" : "PATIENT IN",
"date" : ISODate("2017-01-20)
}
{
"_id" : ObjectId(""),
"patientId" : "200",
"place" : "2",
"action" : "MEDICINE",
"date" : ISODate("2017-01-05")
}
{
"_id" : ObjectId(""),
"patientId" : "300",
"place" : "2",
"action" : "DIAGNOSED",
"date" : ISODate("2017-01-31")
}
EDIT - mapreduce
> var map = function(){emit(this.place,1)}
> var reduce = function(key,values){var res = 0;values.forEach(function(v){res+=1});return{count:res};}
> db.new.mapReduce(map,reduce,{out:"mapped_places"});
{
"result" : "mapped_places",
"timeMillis" : 88,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 2,
"output" : 2
},
"ok" : 1
}
> db.mapped_offices.find({})
{ "_id" : "1", "value" : { "count" : 2 } }
{ "_id" : "2", "value" : { "count" : 2 } }
>
You can try below aggregation query.
db.collection.aggregate([
{
"$group": {
"_id": {
"date": "$date",
"place": "$place"
},
"actions": {
"$push": "$action"
},
"count": {
"$sum": 1
}
}
},
{
"$unwind": "$actions"
},
{
"$sort": {
"_id.date": 1,
"_id.place": 1
}
}
]);
This should output something like
{ "_id" : { "date" : ISODate("2017-01-20T00:00:00Z"), "place" : "1"}, "count" : 2, "actions" : "PATIENT IN" }
{ "_id" : { "date" : ISODate("2017-01-20T00:00:00Z"), "place" : "1"}, "count" : 2, "actions" : "DIAGNOSED" }

Can I use "$first" operator on two fields in a "$group" operation in mongo db?

Consider the dataset
{ "_id" : { "$oid" : "aaa" }, "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 }
{ "_id" : { "$oid" : "bbb" }, "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 }
{ "_id" : { "$oid" : "ccc" }, "student_id" : 0, "type" : "homework", "score" : 14.8504576811645 }
{ "_id" : { "$oid" : "ddd" }, "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 }
{ "_id" : { "$oid" : "eee" }, "student_id" : 1, "type" : "exam", "score" : 74.20010837299897 }
{ "_id" : { "$oid" : "fff" }, "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : { "$oid" : "ggg" }, "student_id" : 1, "type" : "homework", "score" : 21.33260810416115 }
{ "_id" : { "$oid" : "hhh" }, "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 }
Say, for each student, I need to find minimum score and the corresponding document_id(_id).
Here is my pipeline
pipeline = [
{"$sort":{"student_id":1,"score":1 } },
{"$group": {"_id":"$student_id","mscore":{"$first":"$score"},"docid":{"$first":"$_id"} } },
{"$sort":{"_id":1}},
{"$project":{"docid":1,"_id":0}}
]
While this is working fine, I am not sure whether it is because I have the right query or whether it is because of way data is stored.
Here is my stragery
Sort by student_id, score
Group by student_id and do first on score, it will give student_id, min_score
Now, I need the doc_id(_id) also for this min_score, so I am using first on that field also. Is that correct?
Let's say after the sort, I need the entire first document, so should I apply first on each and every field or is there other way to do this?
To get the entire first document after sorting, apply the $first operator on the system variable $$ROOT which references the root document, i.e. the top-level document, currently being processed in the $group operator pipeline stage. Your pipeline would look like this:
var pipeline = [
{
"$sort": { "score": 1 }
},
{
"$group": {
"_id": "$student_id",
"data": { "$first": "$$ROOT" }
}
},
{
"$project": {
"_id": "$data._id",
"student_id": "$data.student_id",
"type": "$data.type",
"lowest_score": "$data.score"
}
}
]

MongoDB MapReduce Standard Deviation over date range

Good Afternoon SO, I wonder if anybody could help me. I am currently investigating using MongoDB Aggregation Framework and MapReduce functions.
My dataset looks like this
[{
"Name" : "Person 1",
"RunningSpeed" : [{
"Date" : ISODate("2005-07-23T23:00:00.000Z"),
"Value" : 10
}, {
"Date" : ISODate("2006-07-23T23:00:00.000Z"),
"Value" : 20
}, {
"Date" : ISODate("2007-07-23T23:00:00.000Z"),
"Value" : 30
}, {
"Date" : ISODate("2008-07-23T23:00:00.000Z"),
"Value" : 40
}
]
}, {
"Name" : "Person 2",
"RunningSpeed" : [{
"Date" : ISODate("2005-07-23T23:00:00.000Z"),
"Value" : 5
}, {
"Date" : ISODate("2006-07-23T23:00:00.000Z"),
"Value" : 10
}, {
"Date" : ISODate("2007-07-23T23:00:00.000Z"),
"Value" : 20
}, {
"Date" : ISODate("2008-07-23T23:00:00.000Z"),
"Value" : 40
}
]
}, {
"Name" : "Person 3",
"RunningSpeed" : [{
"Date" : ISODate("2005-07-23T23:00:00.000Z"),
"Value" : 20
}, {
"Date" : ISODate("2006-07-23T23:00:00.000Z"),
"Value" : 10
}, {
"Date" : ISODate("2007-07-23T23:00:00.000Z"),
"Value" : 30
}, {
"Date" : ISODate("2008-07-23T23:00:00.000Z"),
"Value" : 25
}
]
}
]
I have done a lot of research and as I an see there is no out of the box support for doing SD calculations. I have reviewed a few links and SO posts and came up with this URL https://gist.github.com/RedBeard0531/1886960, which seems to be what I am looking for.
So enough about the background what I would like to do is generate a chart of SDs over each year.
The current function does not take inconsideration each year only the value as a whole. I have changed the map function to and have no idea where to put the group date function.
function map() {
emit(1, // Or put a GROUP BY key here
{sum: this.RunningSpeed.value, // the field you want stats for
min: this.RunningSpeed.value,
max: this.RunningSpeed.value,
count:1,
diff: 0, // M2,n: sum((val-mean)^2)
});
}
However I just get zero's. Could anybody help me adapt this function?
You need to use getFullYear() and a forEach loop through each RunningSpeed entry:
function map() {
this.RunningSpeed.forEach(function(data){
var year = data.Date.getFullYear();
emit(year, // Or put a GROUP BY key here, the group by key is Date.getFullYear()
{sum: data.Value, // the field you want stats for
min: data.Value,
max: data.Value,
count: 1,
diff: 0, // M2,n: sum((val-mean)^2)
});
});
}