I'd like to group and average on dynamic time intervals. Similar question to: Grouping into interval of 5 minutes within a time range.
Right now, I am using the query:
r.db('windMachine').table('stats3')
.between(r.time(2015, 5, 7, 18, 0, 0, "Z"), r.time(2015, 5, 7, 24, 0, 0, "Z"), {index: "date"})
.group([r.row("date").hours(), r.row("date").minutes()])
.avg("TMP0")
Which returns:
[
{
"group": [
21,
2
],
"reduction": 22.802153846153846
},
{
"group": [
21,
3
],
"reduction": 22.6628
},
{
"group": [
21,
4
],
"reduction": 22.384324324324318
}
]
I'd like to group this further, down into 5 minute (or other) intervals. What is the best way to go about doing this?
I actually met a similar problem and here is what I did:
.group(r.row('date').toEpochTime().sub(r.row('date').toEpochTime().mod(<INTERVAL_IN_SECONDS>))
What this do is to group time by <INTERVAL_IN_SECONDS>
I don't know if this is the best way for the task but it works for me.
Related
I am using jsonb_recordset function to convert a json object keys to a new record. I am able to do that for a top level keys. How to do the same for the nested keys?
screener_domain_results
[
{
"index": 0,
"score": 24,
"domain_id": "f758dc46-5107-40b1-ac54-b2419961b721",
"is_concern": true,
"total_count": 8,
"total_score": 40,
"sub_domian_results": [
{
"index": 0,
"score": 15,
"is_concern": false,
"total_count": 5,
"total_score": 25,
"sub_domain_id": "04bea66c-781f-48d6-9bbf-a39961e8dc7c",
"sub_domain_result_range": {
"range_max": 16,
"range_min": 5,
"range_meta": [],
"range_name": "Excelling",
"range_color": "#FFF400",
"entity_ref_id": "04bea66c-781f-48d6-9bbf-a39961e8dc7c",
"range_entity_type": "SubDomain"
},
{
"index": 1,
"score": 10,
"is_concern": false,
"total_count": 5,
"total_score": 25,
"sub_domain_id": "04bea66c-781f-48d6-9bbf-a39961e8dc7d",
"sub_domain_result_range": {
"range_max": 16,
"range_min": 5,
"range_meta": [],
"range_name": "Excelling",
"range_color": "#FFF400",
"entity_ref_id": "04bea66c-781f-48d6-9bbf-a39961e8dc7c",
"range_entity_type": "SubDomain"
} ]
By running the below query i am able to get the 2 columns domain_id and score...But i want to have subdomain_id and score as well which is in the nested object.
select *
from cte
,jsonb_to_recordset(cte.screener_domain_results)
as items(domain_id text, score text );
I have tried to get the nested key like
select *
from cte
,jsonb_to_recordset(cte.screener_domain_results)
as items(domain_id text, score text , sub_domian_results->subdomain_id text , sub_domian_results->score text);
But getting a syntax error. Kindly advise to get this resolved.
I am looking to store some time series data into mondodb. The system outputs data every minute and as such I have made an array called hours. This array is 24 in size (represeing hours in the day). Each hour contains an array of size 60 (every minute of that hour).
The example below shows 5 hours with each hour having only 3, 3,5,3,2 minutes of data respectively.
At the end of each day I will update the document with hourly averages etc...
I did it like this as when it maps to a POJO it is very easy to find the correct hour and minute just by using the array indexes.
Good Link.
http://blog.mongodb.org/post/65517193370/schema-design-for-time-series-data-in-mongodb
The use case is simply to allow charting at the various granularities. Everything from minutes to days.
{
"_id": ObjectId("55eee516b932c564bc8dd645"),
"name": "James",
"hours": [
[
1.12,
2.47,
3.25
],
[
4.12,
5.24,
6.21
],
[
7.25,
8.69,
9.54,
NumberInt(5),
6.36
],
[
10.55,
11.45,
NumberInt(12)
],
[
13.14,
14.23
]
]
}
Does using the array approach over the nested document as in the link example cause any issues when updating as so and when averaging etc...
db.metrics.update(
{
timestamp_hour: ISODate("2013-10-10T23:00:00.000Z"),
type: “memory_used”
},
{$set: {“values.59.59”: 2000000 } }
)
H,
I'm trying to update the version field in this object but I'm not able to make a query with 2 nested $match. So what I would like to do is get the record with file id 12 and version 1.
I would ask also if is it a good practice have more the one nested array in mongoDB (like this object)...
Query:
db.collection.find({"my_uuid":"434343"},{"item":{$elemMatch:{"file_id":12,"changes":{$elemMatch:{"version":1}}}}}).pretty()
Object:
{
"my_uuid": "434343",
"item": [
{
"file_id": 12,
"no_of_versions" : 1,
"changes": [
{
"version": 1,
"commentIds": [
4,
5,
7
]
},
{
"version": 2,
"commentIds": [
10,
11,
15
]
}
]
},
{
"file_id": 234,
"unseen_comments": 3,
"no_of_versions" : 2,
"changes": [
{
"version": 1,
"commentIds": [
100,
110,
150
]
}
]
}
]
}
Thank you
If you want the entire documents that satisfy the criteria returned in the result, then I think it's fine. But if you want to limit the array contents of item and changes to just the matching elements, then it could be a problem. That's because, you'll have to use the $ positional operator in the projection to limit the contents of the array and only one such operator can appear in the projection. So, you'll not be able to limit the contents of multiple arrays within the document.
I'm learning mongo and I have this schema below and i would like some help defining a query:
I would like to get the sum of the "entregado" fields that match this code: 151001. In this case i would get this result = 38.
Do I need to change the schema or is easy to get a query for what i want?
{
"_id": 101,
"torre": 1,
"standard": {
"mamposteria": [
{
"codigo": 311017,
"descripcion": "LADRILLO ARCILLA H-10",
"cantidad": 1080,
"um": "UN",
"entregado": 1080,
"fecha": new Date('June 10, 2013'),
"vale": [1322]
},
{
"codigo": 311021,
"descripcion": "LADRILLO ARCILLA H-7",
"cantidad": 200,
"um": "UN",
"entregado": 200,
"fecha": new Date('June 10, 2013'),
"vale": [1322]
},
{
"codigo": 151001,
"descripcion": "CEMENTO GRIS 50 KG",
"cantidad": 17,
"um": "KG",
"entregado": 17,
"fecha": new Date('June 10, 2013'),
"vale": [1322]
}
],
"mortero": [
// . . .
],
"estructura":[
// . . .
]
}
}
To get the count alone u don't need to modify your schema.
Just following the below steps can give you the count what u need. I will not give you the whole query to find the count. That makes u lazy..so will give steps to get the count...
1) use $unwind 2) use $match to find the value for code: 151001 3) use $group to $sum the values present in entregado
This should give you the count.
If you need any other help pls contact me I will help
I am working on a Mongo database, where I need users to be able to specify dates when they won't be available. I started with this structure:
{
"_id": "demo-spe",
"SC": [ { "SS": 14, "SA": [ 2, 3, 5 ] } ],
"SU": [
{ "IY": 2013, "IM": 12, "ID": 30, "H0": 0, "N0": 0, "H1": 23, "N1": 59 },
{ "IY": 2013, "IM": 12, "ID": 31, "H0": 0, "N0": 0, "H1": 23, "N1": 59 }
]
}
Using this structure, if I want to know which users will be available on a certain date, that is, the "SU" array should NOT have a document that matches IY:(year), IM:(month) and ID:(day). I am really lost with $and and $nin, could please someone guide me?
Thanks, and sorry for the noob question and the non-descriptive fields! :)
You can use a combination of $elemMatch and $not to do this. $elemMatch to match against multiple fields in the same array element, and $not to perform a logical NOT on the expression:
db.test.find({SU: {$not: {$elemMatch: {IY:2013, IM:12, ID:29}}}})