如何将新对象添加到 mongo 中的现有数据
How to add new object to existing data in mongo
我的 table UserCollection:
上有这样一个文档
{
"FirstName": "Mohammad",
"LastName": "Waheed",
}
我想向其中添加另一个数组对象,我该怎么做呢?
"PhoneNumbers":[ //this object is not present in the table
{
"number": NumberInt(8332045764)
},
{
"number": NumberInt(1234567890)
}
]
我的预期结果是这样的:
{
"FirstName": "Mohammad",
"LastName": "Waheed",
"PhoneNumbers":[
{
"number": NumberInt(8332045764)
},
{
"number": NumberInt(1234567890) //when i call this method again it should store like this
}
]
}
这是我试过的:
1.only 获取那条记录
2.
$number 有 phone number
的数据
return $db->update(array('$set' => array("PhoneNumbers.$.number" => $number)));
您可以使用 $push 命令将多个号码添加到列表
model.update({'FirstName':'Mohammad'}, {$push:{PhoneNumbers: NumberInt(1234567890)}}, function (err, updatedModelObj) {
if (err) {
console.log(err);
}
callback(updatedModelObj);
})
我的 table UserCollection:
上有这样一个文档{
"FirstName": "Mohammad",
"LastName": "Waheed",
}
我想向其中添加另一个数组对象,我该怎么做呢?
"PhoneNumbers":[ //this object is not present in the table
{
"number": NumberInt(8332045764)
},
{
"number": NumberInt(1234567890)
}
]
我的预期结果是这样的:
{
"FirstName": "Mohammad",
"LastName": "Waheed",
"PhoneNumbers":[
{
"number": NumberInt(8332045764)
},
{
"number": NumberInt(1234567890) //when i call this method again it should store like this
}
]
}
这是我试过的: 1.only 获取那条记录 2. $number 有 phone number
的数据return $db->update(array('$set' => array("PhoneNumbers.$.number" => $number)));
您可以使用 $push 命令将多个号码添加到列表
model.update({'FirstName':'Mohammad'}, {$push:{PhoneNumbers: NumberInt(1234567890)}}, function (err, updatedModelObj) {
if (err) {
console.log(err);
}
callback(updatedModelObj);
})