This should be a fairly simple one.
myobject
has various properties, _id, name, createdBy, date
etc
In my find query I want to only return specific fields from within myObject. So for example, what would I need to do to modify the find query below so that only name
was returned?
myCollection.find({createdBy: someId}, {fields: {myObject: 1}}).fetch();
Currently this will return everything in myObject
which it should do, I just want one field within myObject
returned.
Here is a way to do it within the query:
myCollection.find({createdBy: someId}, {fields: {'myObject.name':
1}}).fetch();
Note the quotes around
'myObject.name'
Lets assume we are talking about posts, and a post document looks like this:
{
_id: 'abc123',
title: 'All about meteor',
author: {
firstName: 'David',
lastName: 'Weldon'
}
}
You can then extract all of the last names from all of the authors with this:
var lastNames = Posts.find().map(function(post) {
return post.author.lastName;
});
Modify the selector and options as needed for your collection. Using fields
in this case may be a small optimization if you are running this on the server and fetching the data directly from the DB.