在不删除或覆盖现有字段的情况下更新流星集合

编程入门 行业动态 更新时间:2024-10-18 20:32:40
本文介绍了在不删除或覆盖现有字段的情况下更新流星集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

在不删除或覆盖现有字段的情况下更新流星集合

本文发布于:2023-11-30 08:13:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   流星   情况下

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!