How to access and change a specific attribute in a

2019-09-12 23:29发布

问题:

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?

回答1:

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.