简体   繁体   English

如何在Mongodb中附加对象的属性?

[英]How to append attribute for object in Mongodb?

Example: 例:

user : {
    "user_name" : "chicken_01",
    "password" : "123456",
    "skills" : {
        "PHP" : 7.0,
        "NodeJs" : 8.0,
        "MongoDB" : 8.0
    }   
}

I want to add "HTML/CSS" : 8.0 to "skills" inner object. 我想将"HTML/CSS" : 8.0"skills"内部对象。 What is the proper way to do it? 正确的做法是什么? Thank you! 谢谢!

is it like JS? 像JS吗? if yes : 如是 :

var userObj= JSON.parse(user);
userObj.skills.HTMLCSS = 8.0;
user = JSON.stringify(userObj);
db.users.update(
                 {'user_name' : 'chicken_01'}, 
                 {'$set' : 
                        {
                         "skills.HTML/CSS":8.0
                        }
                 })

Except with the name of your collection and not db.users . 除了集合名称而不是db.users

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM