我有一个从客户端调用的 Meteor 方法,该方法依次更新单个文档中的所有字段。在下面的最后一行代码中,我尝试立即在客户端上运行相同的更新以获得立即的副作用(假设更新将会完成)。问题是我得到:
update failed: Access denied. In a restricted collection you can only update documents, not replace them. Use a Mongo update operator, such as '$set'
当尝试这样做时。我不想更新单个字段,我想更新整个文档。有没有办法正确地做到这一点?
entry = {
title: title
text: text
tags: entry.tags
mode: $('#mode').val()
file_ids: entry.file_ids
}
eid = Session.get('entryId')
entry._id = eid if eid
context = Session.get('context')
Meteor.call('saveEntry', title, entry, context)
Entries.update({_id: entry._id}, entry)
最佳答案
做类似的事情:
Entries.update({_id: entry._id}, { $set: entry })
典型的更新请求位于表单中:
Collection.update(
<query>,
<update>
)
来自 Mongodb 文档,关于使用常规 key:value 对象进行更新:
Replace Document Entirely
If the document contains only field:value expressions, then:
- The update() method replaces the matching document with the document. The update() method does not replace the _id value. For an example, see Replace All Fields.
- update() cannot update multiple documents.
以及关于使用 $set
、$inc
等更新特定字段:
Update Specific Fields
If the document contains update operator expressions, such as those using the $set operator, then:
- The document must contain only update operator expressions.
- The update() method updates only the corresponding fields in the document. For an example, see Update Specific Fields.
此外,据我所知,从您的错误消息来看,Meteor 不允许从客户端完全替换整个文档。因此,一个选项是使用 $set 运算符并设置所有值,在本例中是整个文档本身。在将 _id
键传递给 $set
运算符之前,您通常可以跳过将 _id
键添加到文档中。
关于javascript - Meteor js,如何获得更新整个客户端文档的副作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23897612/