In Collection.find, how to format .limit, .sort, fieldlist, and variable column names

advertisements

In non Meteor Server-Side calls to mongodb it is possible make the following chained-option call to the database

    collection.find( { myField: { $gte: myOffset } ).limit( myLimit ).sort( { mySortField : 1 } );

where myField, myOffset, myLimit and mySortField may be resolved from elsewhere at run-time.

This pattern is very useful to create such a run-time generated generic query.

Meteor seems to insist on the non-chained options pattern of

    collection.find( { { myField: { $gte: myOffset } }, { limit: myLimit, sort: { mySortField : 1 }} );

and I am having problems 'building up' a working Find Query as required above from js objects as described in previous questions 17362401 and 10959729

Would anyone like to help?


Edited to show usage of variable:

I do it this way. You send two hashes, where the first is the where clause, and all else are peer level keys.

var locations;
var myfield = 'gps';

search = {
  sureties: {
    $in: sureties
  }
}

search[myfield] = {
    $near: this.gps,
    $maxDistance: kilometers
  };

locations = Agents.find(search, {
  fields: {
    name: 1,
    phone: 1
  },
  limit: limit,
  sort: { field1 : 1 }
}).fetch();