如何访问和更改 collection 中的特定属性?

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

我有一个叫 "Products" 的 collection。我想访问和更改 collection.

中名为 "screenShots" 的属性

此代码不适用于我

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

当我 console.log sshots 时,我可以看到数组存在,但更新功能不起作用

如何将 Product collection 中的 screenShots 属性设置为 "screenshotsURLS" 函数中传递的任何值?

为此,您必须更新 mongodb 文档。

这是在 meteor 中更新文档的方法。

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

在您的案例中,collectionName 是 Products,字段是 screenShots。 所以为了这样做。您的查询将是 sshots

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

要选择文档更新,请使用类似的查询。

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

有关更新的更多信息,请查看此 link