How to insert new object without deleting previous one

坚强是说给别人听的谎言 提交于 2021-02-19 07:44:09

问题


I'm setting up my new RocketChat Server.

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

We have old accounts.

I can add the CAS feature by doing this :

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

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 - Enter in rocketchat_mongo docker

2 - Connect with admin login and use rocketchat database

3 - Execute this code

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

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

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

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.


回答1:


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 }}})


来源:https://stackoverflow.com/questions/55596504/how-to-insert-new-object-without-deleting-previous-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!