Subscribe to a child array of the collection document

I have a collection which represents the uploaded files for the user with a structure like so:

{
_id : SKHdspjhs92346
files : [ { file_name : “1.txt” , file_path : “/home/test1”/} , { file_name : “2.txt” , file_path : “/home/test2”/} ]
}

I want to subscribe to this collection document array changings.

So when client uploads a file im doing a push :

AttachmentsList.update({_id : id},
{ $push: { files : {file_name : fileName, file_path: filePath}}});

And a pull when deleted:

AttachmentsList.update( {"_id": id }, {"$pull": { “files” : {file_name : fileName,file_path: filePath} } } );

I want the elements dinamically change in template. Is it possible to do so? Currently i have this publisher

Meteor.publish(“attachments_list_limited”, function (count,id) {

    var test =  AttachmentsList.find({_id : id}, { "files.$": 1 },{limit: count}, {sort: {"files.file_name": -1}});
    console.log("changed, gout elements: " + test.count());
    return test;
});

And there is a page on client, where i want to output the files from this collection document, selected by _id. Currently the code is not reactive, how can i observe child array object updates?

Check out https://github.com/peerlibrary/meteor-reactive-publish?

1 Like