如何在不删除前一个对象的情况下插入新对象 [英] How to insert new object without deleting previous one

查看:51
本文介绍了如何在不删除前一个对象的情况下插入新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置我的新 RocketChat 服务器.

I'm setting up my new RocketChat Server.

问题是实际上,如果不是由CAS创建的,Rocket Chat不支持CAS帐户.

The problem is that actually, Rocket Chat doesn't support CAS Account if they are not created by CAS.

我们有旧帐户.

我可以通过这样做来添加 CAS 功能:

I can add the CAS feature by doing this :

在Windows上用MongoDB Compass软件进入MongoDB数据库,用键盘在services对象中添加cas对象...

Enter in the MongoDB Database with MongoDB Compass software on Windows, and add cas object in services object with my keyboard...

https://i.imgur.com/AIMAlA6.png

如您所见,我可以通过这样做来添加 CAS 功能.

So as you can see I can add the CAS feature by doing this.

我想用代码做到这一点,所以我这样做了:

I want to do that with code so I did this :

1 - 进入 Rocketchat_mongo docker

1 - Enter in rocketchat_mongo docker

2 - 使用管理员登录连接并使用 Rocketchat 数据库

2 - Connect with admin login and use rocketchat database

3 - 执行这段代码

db.users.update({"name":"Fabien Rousseau"},{ $set: {"services" : { "cas" : { "external_id" : "fabien.rousseau", "version" : 2 }}}})

这个命令是可以的,因为在服务中添加了 CAS 对象,但是,它删除了其他对象,如密码和恢复......

The command is OK because CAS object is added in services, BUT, it delete the other object like password and resume...

我只想添加CAS对象而不删除其他对象...

I just want to add CAS object without deleting other objects...

我的预期结果是第一张图片:CAS 帐户.

My expected result is in the first image : account with CAS.

实际上在命令之后我有这个:

Actually after the command I have this :

https://i.imgur.com/vA1i3mx.png

请帮助我调整我的命令以避免删除我的服务对象中的其他对象.

Please help me to tune my command to avoid deleting other objects in my services object.

推荐答案

您正在将整个 services(包括所有其他字段)传递给 $set,并且,作为结果,您正在重置 services 中嵌套的所有字段.尝试使用 dot 仅传递要更新/添加到文档的数据符号.
$set 文档

You are passing the whole services (includes all other fields) to $set, and, as a result, you are resetting all the fields nested inside services. Try passing only data you want to update/add to your document using dot notation.
$set documentation says

要在嵌入文档或数组中指定字段,请使用点表示法.

To specify an field in an embedded document or in an array, use dot notation.

db.users.update({"name":"Fabien Rousseau"},{ $set: { "services.cas" : { "external_id" : "fabien.rousseau", "version" : 2 }}})

这篇关于如何在不删除前一个对象的情况下插入新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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