mongodb - Mongo group command with missing keys -
I am using Mongo as a tick database where I periodically keep data from time to time. . Later I use this data to learn about the data about the system.
I have such a collection:
[["date": ISOAD ("2014-06-16T03:" minimum ": 4," max ": 25} , {"Date": ISOAD ("2014-06-18T03: 48: 58.152Z"), "Minimum": 3, "Maximum" ": 8}, {" Date ": ISOAD (" 2014-06-18T 03 I group
to group all the documents in the daytime: "48: 58.152 Jade"), "minimum": 8, "max": 12}]
I use the command.
db.stats.group ({keyf: function (doc) {var date = new date (doc) (date, date);} reduce: function ( Curr, result) {result.max = Math.max (curr.max, result.max); date.setHour S (0, 0, 0, 0); || 0); result.m = Math. Middle (curr.min, result.min || 0);}, initial: {}});
This gives me the result:
[["Date": ISOAD ("2014-06-16 T 21: 00: 00.000Z" ), "Minimum": 4, "max": 25}, {"date": ISOAD ("2014-06-18T21: 00: 00.000Z"), "minimum": 3, "max": 12}] < / Code>
The problem for me is that since 2014-06-17 there were no entries, in any way, with the minimum: 0 and maximum: 0
One needs to be created, so the result should be ("2014-06-16T21: 00: 00.000Z"), "min": 4, "max": 25}, {"date": ISODate ("2014-06 -17T21: 0 0: 00.000Z ")," Minimum ": 0," Max ": 0}, {" Date ": ISOAD (" 2014-06-18T21: 00: 00.000Z "," Minimum ": 3," Max. ": 12}]
If I can somehow tell the group command as an array of stars which can do this, if this can be possible in Mongo?
If I could somehow give a array of time to group command keys could do this If this is possible in Mongo?
You can not have the group ()
on the data, so you have to enter some data for each day.
The approach you want to take effectively is that you can ensure that there is a sample every day in your date range.
The problem for me is that there were no entries on 2014/06/17, I need to somehow create at least one one: 0 and maximum: 0
If you are calculating daily minimum / maximum, keep an eye out of the benefits of new and updated operators in MongoDB 2.6 and unload prices in the daily collection.
Since you only want to get a mix / maximum value per day, a daily document might look like this:
{"date": "yyyy-mm-dd" , "Min": 0, "max": 0}
Note: I have chosen the format for clarity in this example as an ISO8601-ish string, but if You can also use a date
field if you wish to include time and timezone information in a complete BSN date
New you must be sure to properly lower your query / update.
Here is an example in Since it is using a You can use the same update query to keep your daily minimum / maximum because new data is available . Db.ticks.find (// query criteria {date: {$ Gte: ISODate ("2014-06-18"), $ for to repeat several dates to open and add any missing values (for convenient date manipulation):
), or calculate daily minimum / maximum count of sort order from original specimens (
var nextDate = moment ("2014-06-01"); Var last date = moment ("2014-07-31"); While updating (next day & lt; = the last day) {db.daily.findAndModify ({query: {date: nextDate.format ("YYYY-MM-DD"}}, update: {$ max: {min: 0, Max: 0},}, upside down: true}}; NextDate.add (1, 'days'); }
upstate
flag:
minimum
and maximum
field values (i.e. existing values will be preserved) instead of 0 for the minimum / maximum of 0):
Daily minimum for 2014-06-18 (suppose ticks are using the standard 'date` field) db.ticks.find (// query criteria {date: {$ gte: ISODate ("2014- 06-18 "), $ lieutenant: ISOAD (" 2014-06-19 ")}, / projection {min: 1, _id: 0}) .sort ({min: 1}). Border (1)
daily daily 2014-06-18 Lieutenant: ISOAD ("2014-06-19")}}, // Projection {max: 1, _id: 0}). SORT ({MAX: - 1}). Border (1)
Comments
Post a Comment