Question

One can easily manage existance of some fields in db.find()'s result set by specifying fields parameter (I use Meteor and test all queries on server side, in publishing function). Say Meteor.collection.find({}, { fields: { 'a': false } }) tells Mongo to exclude field a from the result set.

However, if a is a subdocument, this doesn't work. Say, it has two fields: x and y. If I specify { 'a.x': false }, one of these fields is excluded, but not the other. If I add { 'a': false}, nothing happens.

Here is the question: is there any way to filter out from result set all fields that lie in some particular subdocuments of a document?

Was it helpful?

Solution 2

The problem was that I've added both subdocument's name and its fields names, like this: {fieldB: false, 'fieldB.sub1': false}. In this case, mongo ignores the outer exclusion and uses inner one. Removal of everything enclosed in fieldB solves the problem.

By the way, it's Mongo's behavior, not Meteor's binding's. You can easily reproduce it. Say, you have document with a field fieldA, which is a subdocument with two fields: sub1 and sub2.

// Returns _id only
db.items.find({}, {fieldB: false});

// Returns fieldB.sub2
db.items.find({}, {fieldB: false, 'fieldB.sub1': false});

OTHER TIPS

Only works consistently with either inclusion or exclusion - not both at the same publish select

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top