I am able to update an array inside a document using $addToSet, but I want to update an array which is inside a document array.
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);
}
})
});
Now I want to update(if exists)/insert(if don't exist), 1. Value 2. keywords of a particular value
Thanks in advance !!!
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);
}
})
});
By the way, you can query your Entity
db by using a simpler syntax. Entity.update()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.