What is the required syntax for a MongoDB find/fetch?

I want to retrieve Documents from a Collection where the value of one field equates to a specific value (specifically, where the Document field “wjllu_workerid” equals the passed-in arg “workerid”), but I only want one field actually returned, expressly “wjllu_jobloc”).

The commented-out attempts to do this fail; the other one compiles, but does not give me the needed “jobloc” value:

Meteor.publish("workersJobLocsLookupByWorker", function (workerid) {
    // return WorkersJobLocsLookup.find( {wjllu_workerid: workerid}, {fields: {wjllu_jobloc}});
    return WorkersJobLocsLookup.find( {wjllu_workerid: workerid}.fetch();
    //return WorkersJobLocsLookup.find( {wjllu_workerid: workerid, wjllu_jobloc: true}.fetch();

Does anybody have an example of how to do this (restrict the document count of a result set based on a failter, but also restrict the “width” of each Document by restricting the fields returned)?

First you should return a cursor inside Meteor.publish, so you souldn’t apply fetch(), just find(). (See the docs for find and fetch).

Second, Meteor mongo query looks like find({<selector>}, {fields: {<fields restriction>}, skip: <skip docs count>, limit: <limit docs count>}). For example I wanna publish posts list by author id, and pass only 3 records with title field only:

Meteor.publish('postsByAuthor', function (authorId) {
  check(authorId, String);
  Posts.find({author: authorId}, {fields: {title: 1}, limit: 3});
1 Like

Okay, after sleeping on this and reading your reply, I think what I really need is:

return WorkersJobLocsLookup.find( {wjllu_workerid = workerid, wjllu_createdby = this.userid}, { fields: { wjllu_jobloc: true }} );

The logic behind this is that I only want Documents for the selected worker, where the Document was created by the current user, and the only field I want to be returned from the Documents that meet those two criteria is “jobloc”.

Does my updated “Non-SQL” look right?

Except for this.userId

I suggest studying http://docs.meteor.com/#/full/selectors

So what should this.userid be instead?

it should be this.userId, not this.userid

1 Like

So this is my new idea based on mrzafod (who looks a little like Aaron Rodgers)'s and narven’s answers:

Meteor.publish('getJobLocsForWorker', function (workerid) {
  check(workerid, String);
  WorkersJobLocsLookup.find({wjllu_workerid: workerid, wjllu_createdby: this.userId}, {fields: {wjllu_jobloc: 1}, limit: 420000000});

(assuming I want up to 42 million Documents returned).