0

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"
}
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
bcbishop
  • 2,193
  • 3
  • 20
  • 23
  • Could you provide some more info: 1. # documents you plan to retain in this collection 2. is this collection sharded, and if so, what is the current key 3. How often will you be querying for the node's latest heartbeat? 4. any other critical queries. Thanks – Dylan Tong Jul 26 '13 at 01:02

2 Answers2

2

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.

Community
  • 1
  • 1
titogeo
  • 2,156
  • 2
  • 24
  • 41
0

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.

Chris Chang
  • 426
  • 1
  • 3
  • 7