我可以使用$ addToSet更新文档中的数组,但我想更新文档数组中的数组。
const mySchema1 = new Schema({
myId : {type:String, unique:true},
name : String,
entries : [{
value : String,
keywords:[String]
}]
});
app.put('/api/entity',function(req,res){
let model = new Entity();
console.log(req.body);
model.collection.update({"myId":req.body.myId,"entries":req.body.entries},{$addToSet:{"entries":{$each:[req.body.entries]}}},function(err,entries){
if(err){
res.send(err);
}else{
res.send(entries);
}
})
});
现在我想更新(如果存在)/插入(如果不存在), 1.价值 2.特定值的关键字
提前致谢!!!
答案 0 :(得分:0)
var entries = req.body.entries;
// match ALL of the documents whose `entires` attribute contains ALL `req.body.entries`
var query = {"myId":req.body.myId, "entries": {$all: entries}};
// match SOME of the documents whose `entires` attribute contains SOME `req.body.entries`
// var query = {"myId":req.body.myId, "entries": {$some: entries}};
// ADD all `req.body.entries` to the matched documents
var update = {$addToSet:{"entries":{$each:[entries]}};
// Create a new matching document if one doesn't already exeist
var options = {upsert = 1};
app.put('/api/entity',function(req,res){
let model = new Entity();
console.log(req.body);
model.collection.update(
query,
update,
options
},function(err,entries){
if(err){
res.send(err);
}else{
res.send(entries);
}
})
});
顺便说一句,您可以使用更简单的语法查询Entity
db。 Entity.update()