我不知道为什么,但是如果我尝试使用$set方法更新现有字段,则所有现有字段都将在相同的上下文中替换.
I don't know why but if i try to update an existing field using the $set method, any existing fields are replaced in the same context.
例如.说我有一个包含以下字段的现有集合.
For example. Say i have an existing collection with the following fields.
馆藏名称:Ticket
{profile: {name: "Test", placement: 1}, requestor: _id}当我尝试向此集合中添加/更新字段时,如下所示:
When i attempt to add/update fields to this collection like this:
var ticket = Meteor.tickets.findOne({_id: ticketID}); if(ticket){ Meteor.users.update(ticket, { $set: profile: {name: "Test2", new_fields: "value"} }); }该集合会更新,并且名称字段会更改,但会删除放置位置,并且不再存在.如果我删除name字段,也是如此.我们如何正确更新流星集合,而不必不断重复传递相同的结构?
The collection gets updated and the name field changes but placement is removed and no longer there. This is also true if i remove the name field. How do we properly update a meteor collection without having to keep passing the same structure over and over?
推荐答案只需执行以下操作:
$set: {"profile.name": "Test2", "profile.new_fields": "value"}即您正在替换整个哈希.相反,您可以更新哈希中的字段.
I.e. You were replacing the whole hash. Instead you can update the fields within the hash.
更多推荐
在不删除或覆盖现有字段的情况下更新流星集合
发布评论