使用 MongoDB/mongoose 在 WHERE 插入文档
Insert document WHERE with MongoDB/mongoose
我正在尝试将具有相同名称的相同新对象插入到我的集合中
示例;
//BEFORE
{
_id: HasiduhIDuad*7d89adajd,
nickname: "Clark",
password: "hush",
info : {
info1: "test"
}
}
// AFTER
{
_id: HasiduhIDuad*7d89adajd,
nickname: "Clark",
password: "hush",
info : {
info1: "test"
}
info : {
info1: "test"
}
}
// Add in collection WHERE nickname === "Clark" object named "info"
我试过更新,但这是更新当前信息,我需要添加另一个。
尝试过 ;
getCollection.update({name: "Clark"}, {
$set: {
info: {
info: "test"
}
}
})
我怎样才能完成这项工作?
这是不可能的,您需要像这样添加数组;
getCollection.findOne({name: "Clark"}, function(err, data){
data.info.push({info: "test"}); // push in data new object
data.save() // save data with new object
});
不要忘记将 info
所在的架构更改为 info: []
我正在尝试将具有相同名称的相同新对象插入到我的集合中
示例;
//BEFORE
{
_id: HasiduhIDuad*7d89adajd,
nickname: "Clark",
password: "hush",
info : {
info1: "test"
}
}
// AFTER
{
_id: HasiduhIDuad*7d89adajd,
nickname: "Clark",
password: "hush",
info : {
info1: "test"
}
info : {
info1: "test"
}
}
// Add in collection WHERE nickname === "Clark" object named "info"
我试过更新,但这是更新当前信息,我需要添加另一个。
尝试过 ;
getCollection.update({name: "Clark"}, {
$set: {
info: {
info: "test"
}
}
})
我怎样才能完成这项工作?
这是不可能的,您需要像这样添加数组;
getCollection.findOne({name: "Clark"}, function(err, data){
data.info.push({info: "test"}); // push in data new object
data.save() // save data with new object
});
不要忘记将 info
所在的架构更改为 info: []