mongo 如果 meteor 的文档中不存在,则插入对象

mongo insert object if not present in doc in meteor

我有这个对象

我有记录

{
     "_id":"xxxx",
     "industry" : "Information Technology and Services",
    "name" : "dsdds",
    "profession" : "Information Technology and Services Profession Two",
    "self_employed" : true,
    "sex" : "M"
}

我想用 {loc:"sss",db:"sss",bio:"fsdf"} 更新对象作为配置文件对象,如下所示

 {
         "user_id":"xxxx",      
         "industry" : "Information Technology and Services",
        "name" : "dsdds",
        "profession" : "Information Technology and Services Profession Two",
        "self_employed" : true,
        "sex" : "M",
        "profile":{loc:"sss",db:"sss",bio:"fsdf"}
    }

我试过这个

User.update({user_id:"xxxx"},{$set:{"profile":{user:"ssa",sss:"dsd"}}})
User.update({user_id:"xxxx"},{$set:{"profile":profile}})

两者都返回 1 但没有插入记录。

我的命令有什么问题? 在我的终端

User.update({user_id:"JvH4YMBJmZrKWPhig"},{$set:{"profile":{user:"ssa",sss:"dsd"}}})
1
> User.findOne({user_id:"JvH4YMBJmZrKWPhig"})
{ _id: 'ye4uzZYXT5PCtcNrs',
  company: 'ghghfh',
  dob: '703621800',
  hubs: [ 'AcztzE8W3hFTeTyz8' ],
  industry: 'Computer Software',
  job_title: 'Software Engineer',
  name: 'ss',
  profession: 'Computer Software Profession One',
  self_employed: false,
  sex: 'M',
  user_id: 'JvH4YMBJmZrKWPhig' }

试试

var finde = User.findOne({user_id:"JvH4YMBJmZrKWPhig"})

所以尝试其中之一。

Meteor.users.update({_id:finde._id}, {$set: {profile: {value1: 'value1', value2: 'value2'}}});

或者如果您使用的是与帐户用户不同的另一个集合 在更新中使用对象。

var values = {
  value:"value1",
  value2:"value2"
}
    User.update({_id:finde._id},{$set: {profile: {values}}});