2016-03-07 67 views
0

我有一个名为“产品”的集合。我想要访问并更改集合中名为“screenShots”的属性。如何访问和更改集合中的特定属性?

此代码并没有和我一起工作

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

当我CONSOLE.LOG的sshots,我可以看到,数组存在,但更新功能无法正常工作

我如何将Product集合中的screenShots属性设置为“screenshotsURLS”函数中传递的任何值?

+0

是否要一次更新所有文档? –

+0

@Blaze Sahlzen不,只有当前文档中的特定元素“screenShots” –

+0

您当前的代码将更新它在集合中找到的第一个文档。你应该在第一个'{}'中指定一个查询,例如'{_id:someId}'。 –

回答

2

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

这是你如何更新流星文件。

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

在你的情况下,collectionName是Products和field是screenShots。 所以这样做。您的查询将 sshots

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

对于选择文档更新使用类似查询。

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

更多关于更新请点击这里link

+0

是否有可能只更改screenShots属性而不更改值,如果其他属性? –

+0

我的意思是如果我使用Products.update({},{$ set:{screenShots:sshots}})它会改变集合中其他元素的值吗? –

+0

否,Products.update({},{$ set:{screenShots:sshots}})只会更改screenShots字段的值。 –

相关问题