在不删除或覆盖现有字段的情况下更新流星集合 [英] Update meteor collection without removing or overriding existing fields

查看:70
本文介绍了在不删除或覆盖现有字段的情况下更新流星集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,但是如果我尝试使用$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.

这篇关于在不删除或覆盖现有字段的情况下更新流星集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆