Search code examples
meteormeteor-collection2

How to access and change a specific attribute in a collection?


I have a collection called "Products". I want to access and change an attribute called "screenShots" inside the collection.

This code didn't work with me

screenshotsURLS: function(sshots) {
    check(sshots, [String]);
    Products.update({},{$set:{screenShots:sshots}});
    console.log(sshots);
}

when i console.log the sshots, i can see that the array exists, but the update function isn't working

how do i set the screenShots attribute inside the Product collection to whatever value passed in "screenshotsURLS" function?


Solution

  • For that you have to update the mongodb document.

    THis is how you can update the doc in meteor.

    collectionName.update(
       <query>,
       <update>,
       {
         upsert: <boolean>,
         multi: <boolean>,
         writeConcern: <document>
       }
    )
    

    In your your case collectionName is Products and field is screenShots. So for doing this. Your query will be sshots

    Products.update({},{$set:{screenShots:sshots}}) (Be careful this will update all of your doc)
    

    For selecting doc update use the query like.

    Products.update({name:'yourProductName'},{$set:{screenShots:sshots}})
    

    For more about update a please check this link.