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

不问归期 提交于 2019-12-12 05:29:26

问题


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.



来源:https://stackoverflow.com/questions/35840617/how-to-access-and-change-a-specific-attribute-in-a-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!