How to filter collection on nested field values


#1

I have a meteor collection with the below structure. This actually comes from a Meteor-files collection by ostrio.

{
 "_id" : "HsXoZ6bxkx5kMcJtm", 
"name" : "trees.jpg", 
"meta" : { "artist_id" : "QkmYdsZsMmRzqTg58" , "artist": "some name"}, 
"mime-type" : "audio/mp3", 
"userId" : "QkmYdsZsMmRzqTg58", 
"_collectionName" : "images" 
}

I define a publication

Meteor.publish('files.artist', function publishUserImages(){
   return Images.find({meta: {artist_id: this.userId}).cursor;
});

I want to filter on the meta key, to return all items with artist_id.
My current filter will get only those Images where meta has the exact value {artist_id: "QkmYdsZsMmRzqTg58"}. The filter won’t return the item shown above since it the meta value has an extra key artist

How do I construct an appropriate filter?


#2

If think you need $elemMatch:
https://docs.mongodb.com/manual/reference/operator/query/elemMatch/
But I am not too much of a MongoWizzard :slight_smile:


#3

Thanks man for the suggestion. But I got what I was looking for on SO.

    Meteor.publish('files.artist', function publishAudios(){
        return Audios.find({'meta.artist_id': this.userId}).cursor;
    });
``