1

collection structure as such:

{"_id" : "abc",
"potentialUsers" : [{"userID" : "def"}, 
                    {"userID" : "ghi"}]
},

{"_id" : "123",
"potentialUsers" : [{"userID" : "456"}, 
                    {"userID" : "789"}]
},

I want to query if user abc has the user def in their potential users array. My current query (client) is

    getPendingLiftRequests: function() {
        return collection.find({}, {potentialUsers: {$elemMatch: {userID: Meteor.user().services.facebook.id}}});
    }

On the server I publish all of that collection to the user, and then just selectively show it based on the view of the client. However, when I try to use an elemMatch on the array, it just shows all of the records, when it should only show 1.

1
  • 1
    I don't know meteor, but that {} as first argument in a mongo find looks pretty suspicious. It looks like an empty query which returns all documents. What is the first argument supposed to be? Commented Sep 7, 2015 at 21:51

2 Answers 2

2

You don't need $elemMatch here. This should work:

var fId = Meteor.user().services.facebook.id;
return collection.find({'potentialUsers.userID': fid});
Sign up to request clarification or add additional context in comments.

Comments

2

Have look at the documentation for minimongo in Meteor: http://docs.meteor.com/#/full/find

You have to use the fields option to get the desired result.

getPendingLiftRequests: function() {
    return collection.find({}, { fields: { potentialUsers: {$elemMatch: {userID: Meteor.user().services.facebook.id}}}});
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.