Meteor - collection.find() always returns all fields

node.jsMongodbMeteor

node.js Problem Overview


Ran into this (slightly annoying problem) I'm trying to look up all records in a collection and not show (or show) a specific field (score). This is just an example and not reflective of my actual code, but the problem is always reproducible. The fields I am excluding are very large and I'm merely trying to generate a menu of available records.

Commands like

    players.find({},{score:1})
    players.find({},{score:0})

Always return every field, instead of triggering the exclude/include in mongodb. Am I worried about nothing, since the template potentially can control what data gets rendered to html? Still feels like the data is transferred to the client side regardless; and shows up in the console.

node.js Solutions


Solution 1 - node.js

your syntax is off a bit, it should be

CollectionName.find({}, {fields: {'onlyThisField':1}});

or

CollectionName.find({}, {fields: {'everythingButThisField':0}});

your template does indeed control what data is displayed, but there are still many scenarios where field limiting makes sense - privacy of data or efficiency (some fields of all records, all fields of the 'current' record) are two common ones

you didnt mention it, but this usually is within a publish function - see http://docs.meteor.com/#meteor_publish - the fields modifier is also available on the client, but there it does not limit data sent down to client, just to the template - server side field reduction/selection has different benefits

--

double check that you've removed the autopublish package too, however you should see a warning if you have that active and write your own publish functions, which is where you would most commonly use fields

Solution 2 - node.js

First, if you want to control some fields in Collection.find(),you can try to do it this way:

CollectionName.find({}, {fields: {field:1}});

but it was working only on the server.

Or try this:

On the server:

Meteor.publish("myCollection", function () {
	return SvseTree.find({},{fields: {field:1}});
});

On the client:

Meteor.subscribe("myCollection");

then run meteor remove autopublish.

Second, if you want to get Array of Collection.find(), try to do it: Collection.find().fetch();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionredcap3000View Question on Stackoverflow
Solution 1 - node.jsnate-strauserView Answer on Stackoverflow
Solution 2 - node.jsL.TView Answer on Stackoverflow