Meteor - collection.find () always returns all fields

Ran into this (slightly annoying problem). I try to find all the entries in the collection and not show (or show) a specific field (rating). This is just an example and does not reflect my actual code, but the problem is always reproducible. The fields that I exclude are very large, and I'm just trying to create a menu of available records.

Commands like

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

Always return each field instead of throwing an / include exception in mongodb. I'm not worried about anything, since the template can potentially control what data is transferred to html? It still seems that the data is transmitted to the client side independently; and is displayed in the console.

+51
mongodb meteor
Apr 12 '13 at 1:06 on
source share
3 answers

your syntax is off, it should be

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

or

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

your template really controls what data is displayed, but there are still many scenarios where the field restriction makes sense - data privacy or efficiency (some fields of all records, all fields of the "current" record) are two common

you did not mention this, but it is usually in the publish function - see http://docs.meteor.com/#meteor_publish - the fields modifier is also available on the client, but there it does not limit the data sent to the client, just to reduce / Selecting a server-side field has different advantages.

-

double check that you also uninstalled the autopublish package, however you should see a warning if you have this asset and write your own publishing functions, in which you most often use fields

+97
Apr 12 '13 at 1:20
source share

Firstly, if you want to manage some fields in Collection.find() , you can try to do it like this:

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

but it only worked on the server.

Or try the following:

On server:

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

On the client:

 Meteor.subscribe("myCollection"); 

then run meteor remove autopublish .

Secondly, if you want to get Array from Collection.find (), try this: Collection.find () fetch () ;.

+10
Apr 12 '13 at 3:56
source share

The Meteor Server method is used for all information about the user, with the exception of user services (since it includes the user password)

 "getUserInfo": function() { let userInfo = Meteor.users.findOne( { _id: Meteor.userId() }, { fields: { services: 0 } } ); return userInfo; } 
0
Dec 05 '18 at 7:22
source share



All Articles