node.js - How to update time series data efficiently in MongoDB -
i have following time series data in mongodb
channels collection has data regarding each channel. each channel document has real time data -> rtdata json array holding time series data channel.
db.channels.find({}).pretty() gives similar structure
{ channelname:"abc", rtdata:[ { ts:iso_date(timestamp), data:[12, 14] }, { ts:iso_date(timestamp), data:[12, 14] }, { ts:iso_date(timestamp), data:[12, 14] }, . . . }, { channelname:"nbc", rtdata:[ { ts:iso_date(timestamp), data:[12, 14] }, { ts:iso_date(timestamp), data:[12, 14] }, { ts:iso_date(timestamp), data:[12, 14] }, . . . },
now, every 4 seconds updated record 1 or more channels
{ ts: iso_date(timestamp), data: [14,15] }
this record need push/update in rtdata array of channel.
so did similar -
channels.findone({query}, function(channel) { channel.rtdata.push(newdata); channels.findandmodify({query}, {$set:{rtdata: channel.rtdata}}, function({})) })
find channel, push data rtdata array , find , modify.
now seems work when data volume low. when there close 50k elements in rtdata array of 1 single channel app not able handle that.
is there efficient update time series data.
you overembedded model, imho. keep in mind there 16mb size limit on bson documents in mongodb. furthermore, modifying document takes lot more time inserting one.
since objectid contains timestamp already , additionally gives uniqueness each entered value, like
{ _id: new objectid(), channelname: "nbc", value: [14,15] }
makes inserting data extremely efficient. querying based on date, use advantage objectids starting hex representation of 4 byte value containing secs since epoch , otherwise monotonically increasing:
secs = math.floor(date.now()/1000) hexsecs = secs.tostring(16) id = objectid(hexsecs+"0000000000000000") db.values.find({"_id":{$lt:id}})
this example give entries older time date.now()
called. can query ranges, of course, using same method of converting beginning , end dates. more detailed explanation, see the according entry "popping time stamps objectids" on kristina chodorow's blog
the rest of queries , aggregations should obvious.
Comments
Post a Comment