Bernd Loigge
Bernd Loigge

Reputation: 1677

Meteor - Query Embedded Documents

I would like to fetch only specific fields in embedded documents of my collection.

One Document of my Collection:

"_id" : "fDa9J245hkKnZyipM",
"OrderID" : "qPypJCWov79dQ2nc2",
    "MWLink" : {
            "LinkType" : "KRF - PPUI - LOO",
            "LinkID" : "test3",
            "SiteA" : "placeA",
            "SiteB" : "placeB"
    }

My helper:

        linkID: function() {
            return MyCollection.find({},{"MWLink.LinkID": 1}).fetch();
        }

I would like to have this result:

{"_id": "fDa9J245hkKnZyipM", "MWLink": {"LinkID": "test3"}},
{"_id": "kioa56245hrTnzuiL", "MWLink": {"LinkID": "test5"}},
....

But I get the whole documents...

Thanks in advance!

Upvotes: 0

Views: 396

Answers (1)

Tarang
Tarang

Reputation: 75945

Use fields (?):

return MyCollection.find({},{fields:{"MWLink.LinkID": 1}}).fetch();

If you feel you need a bit more power on what comes up you can use map (?) or a transform (?):

var transform = function(doc) {
    return {
        MWLink : {
            LinkID: doc.MWLink.LinkID
        }
    }
}


//A transform returns a cursor
return MyCollection.find({}, {transform: transform});

//Map returns an array of documents
return MyCollection.find({}).map(transform);

Upvotes: 1

Related Questions