bcbishop
bcbishop

Reputation: 2313

Mongo query to return the last update time of each node

We have a hundred computers running, each computer will send back a heartbeat once in few minutes. we capture those heart beats in our mongodb database. Now we want to check when was last time they sends back their heart beat. One solution we have is to query for each node and get back its last heart beat time. But that'll introduce same number of queries to the database as the number of nodes we have. We wonder if there is a simpler approach to do that.

To be more specific, we store each heart beat from a node in a separate document, something like the following

{
    "_id" : ObjectId("51d173adedfce2c67fe04c4a"),
    "nodeId" : 260,
    "heartBeat" : NumberLong(1374778030),
    "status" : "DEPLOYED"
}

Upvotes: 0

Views: 2255

Answers (2)

titogeo
titogeo

Reputation: 2184

You can get the time from ObjectId. Query by node id, then sort by ObjectId, and get the timestamp from latest document's objectId. This will be your last ping time.

See Here. and Here.

Upvotes: 2

Chris Chang
Chris Chang

Reputation: 426

Along the same lines of Dylan's comment, you should probably provide some more information for an optimal response. In addition to his comments, one that comes to mind is if you do full scans every time you look for heartbeats. That is, you could potentially group some nodes in a doc as an array (or create new collections based on access patterns) and manipulate in the app layer.

Upvotes: 0

Related Questions