Three things:
MongoDB and Meteor ids are stored in
_id
rather thanid
.In the context of your
forEach
method,host
iterates through the query set returned byHosts.find()
, but it doesn't actually give you access to the documents themselves. Essentially, it's a copy of the information in the MongoDB rather than the document in the database.The correct (and only) way to update the actual document is by using the Collection.update method:
Hosts.update({_id: host._id}, {$set: {lastPing: Pings.findOne({id: host.id}, {sort: {timestamp : -1}}) }});
(note that you can only update by
_id
on the client which is why that's what I've used here, whereas you can supply any query on the server.)The
hosts
object is a cursor rather than an array. This means that when you usefor host in hosts
, you're actually iterating through the properties of the cursor object (which are inherited from the prototype) rather than an array of hosts, and none of them has an id property. One way to make this work is to fetch the query set and put it intohosts
like this:var hosts = Hosts.find().fetch();
Alternatively, you can stick with the cursor and use
forEach
again, although you'll either have to rewind it withhosts.rewind()
, or repeat the line above to reset it to the start of the query set.
Hope that's helpful.