How to query meteor-mongo on value, not _id or key

I am creating a meteor app that gets sensor data from remote devices via POST, and need to query on a value in a mongo collection, returning the document _id.

The document is similar to this:

    	_id: 0,
    	device: {
    		mac: 'XXX', // always unique
    		sd1: nnn,
    		sd2: nnn,

I need to be able to do a findOne() on 'XXX', determine if it exists, and get the _id of the corresponding doc.

I’ve tried variations of collection.findOne({}, {mac: 1}), but that only returns the first document that contains the key ‘mac’. As every doc will contain that key (but the value will always be unique), this is pretty useless as it always returns the first document in the collection. I need to query on the value of the key.

There must be an obvious solution, but it has eluded me thus far.

findOne({mac:'XXX'}); :slight_smile:

Also, lots of good info here:
(Some of the advanced features are not implemented in Meteor, but most work)

Actually, it’s findOne({'device.mac': 'XXX'});

You should also put an index on that field. Typically in the server side Meteor.startup() you would add someCollection._ensureIndex({'device.mac': 1});.

Thanks, that works great… and thanks for the _ensureIndex tip!

1 Like