I am with a project of statistics analysis with Apache logs with MongoDB with Java. Apache logs comes like:
Tue, 13 Feb 2018 11:39:26.081 ;; ProcessId = 28889 ;; IPRequest = 10.160.74.43 ;; IPLocal = 10.160.85.46 ;; SizeResponseBytes = 2968 ;; TimeResponse = 14213 ;; Protocol = HTTP/1.1 ;; Port = 80 ;; Method = GET ;; Url = /login/ ;; Query = ;; HTTPstatus = 200 ;; BytesReceived = 479 ;; ByteSend = 3509 ;; Referer = - ;; ServerName = www.managercapture.com ;; UseCanonicalServerName = 10.160.85.46 ;; User-Agent = Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 ;; SessionID = -
This part:
BytesReceived = 479 ;; ByteSend = 3509
are data in bytes received and sent in a HTTP request.
In Mongo, I have a collection like:
{
date: yyyy/MM/dd HH:00:00
data: [
{second: 1, byteSent: 100, bytesReceived: 200},
{sedond: 44, byteSent: 322, bytesReceived: 150},
...
]
}
Now comes another line with {second: X, byteSent: 555, bytesReceived: 300}
.
I wonder if I can do this in one query:
- Search for the document with
date
anddata.second
, for example,1
or3
. - If found, sum the value of same seconds, to get the total bytes data in one second(in the same second there may be more than one requests). (second 1 has previous data, so sum up:
{second: 1, byteSent: 555+100, bytesReceived: 300+200}
) - If not found, add this document into the list. (second 3 has no previous data, so add the document:
{second: 3, byteSent:555, bytesReceived:300}
.)
Answers like "It is not possible because ..." is also welcome, with ref, etc.