How to return only 1 field of object in Mongo Collection

Hey guys,

I have a Mongo Collection with an array inside, which is itself composed of an object with multiple fields (here’s part of the Schema)

    purchases: {
        type: Array,
        optional: true
    },
    'purchases.$': {
        type: Object,
        optional: true
    },
    'purchases.$.userId': {
        type: String,
        optional: true
    },
    'purchases.$.stripeId': {
        type: String,
        optional: true
    }

Now I want to create a publication that sends only the userId field to the client (but not the stripeId).
Something like (just tested this, it doesn’t work):

Meteor.publish('purchases-userid', function (productId) {
    return Products.find({
        _id: productId
    }, {
        fields: {
            'purchases.$.userId': 1
        }
    });
});

also tried:

Meteor.publish('purchases-userid', function (productId) {
    return Products.find({
        _id: productId
    }, {
        fields: {
            purchases: {
                userId: 1
            }
        }
    });
});

Any idea how I can do this?

Thanks!

Have you also tried

"purchases.userId": 1
1 Like