问题描述
我正在尝试这段代码更新嵌套在bool
模式的Bigpaths
数组中的Addtasks
数组中的所有User
元素。但是我看不到MongoDB中的更改。尽管在控制台中它正在打印“布尔”字符串。我在哪里停留?
router.post('/api/update/:id',function(req,res,next){
User.updateMany( {"Addtasks.Id4AddtasksBigpaths":req.params.id},{ $set: { "Bigpaths.$[elem].bool" : true } },{ arrayFilters: [ { "elem.Id4AddtasksBigpaths": req.params.id} ] },function (error,success) {
if(error)
{
console.log(error);
}
else {
console.log("bool");
}
}
)
})
这是我的用户集合中的Addtasks数组:
[{
"Id4AddtasksBigpaths" : "m9b8mpyn2iby$s","clientName" : "Vikas Yadav","deadline" : "Set Deadline","assignee" : "Assign","displayLock" : "none","displayDelete" : "inline","commonID" : "o5xz13ffxq8","status" : "Requirement Completed","Date" : "Sat Sep 19 2020 00:00:05 GMT+0530 (India Standard Time)","exampleRadios" : "option1","otherdetails" : "trhr","website" : "nmbn.com","keywords" : "anxiety disorders for children,anxiety disorders for adults","words" : 543,"topic" : "How to build the empire?","_id" : ObjectId("5f64fcad7c8b1b252c441360"),"Bigpaths4Clients" : [],"Bigpaths" : [
{
"path" : "public\\files\\Screenshot_2016-11-25-23-19-51.png","name" : "Screenshot_2016-11-25-23-19-51.png","Id4AddtasksBigpaths" : "m9b8mpyn2iby$s","uniqueId" : "8q9tcy2w3k3u$id","bool" : ""
},{
"path" : "public\\files\\Screenshot_2017-11-25-23-21-51.png","name" : "Screenshot_2017-11-25-23-21-51.png","uniqueId" : "9q9tcz2w3k3u$id","bool" : ""
}
]
}]
完整用户架构:
var userSchema = new mongoose.Schema({
email: {
type: String,unique: true,required: true
},name: {
type: String,tag: {
type: String
},hash: String,salt: String,Addtasks : [{
topic: String,words: Number,keywords: String,website: String,otherdetails: String,exampleRadios: String,Date: String,Bigpaths:[],status:String,fileName: String,commonID:String,Bigpaths4Clients:[],displayDelete:String,displayLock:String,assignee:String,deadline:String,completion:String,clientName:String,Id4AddtasksBigpaths:String
}]
});
userSchema.methods.setPassword = function(password) {
this.salt = crypto.randomBytes(16).toString('hex');
this.hash = crypto.pbkdf2Sync(password,this.salt,1000,64,'sha1').toString('hex');
};
userSchema.methods.validPassword = function(password) {
var hash = crypto.pbkdf2Sync(password,'sha1').toString('hex');
return this.hash === hash;
};
module.exports = mongoose.model('User',userSchema);
快速更新代码:
router.route('/profile/view/:id')
.get(function(req,next) {
Editable.findOne({userId: req.params.id},function (err,dataEditable) {
if (dataEditable){
console.log("if condition");
User.findOne({_id: req.user.id},function(err,dataUser) {
if (dataUser) {
console.log("two");
dataUser.Addtasks.forEach(element => {
if(element._id == req.params.id)
{
console.log("userID and paramID got matched in if condition");
res.render('viewTask',{mainData: element,hiding:true});
console.log("viewTask rendered with editing option");
}
});
}
else {
res.render('error');
}
});
}
else {
console.log("else condition");
User.findOne({_id: req.user.id},data) {
data.Addtasks.forEach(element => {
if(element._id == req.params.id)
{
console.log("userID and paramID got matched in else condition");
User.updateMany( {"Addtasks.Id4AddtasksBigpaths":element.Id4AddtasksBigpaths},{ $set: { "Addtasks.$.Bigpaths.$[elem].bool" : true } },{ arrayFilters: [ { "elem.Id4AddtasksBigpaths": element.Id4AddtasksBigpaths} ] },success) {
if(error)
{
console.log(error);
}
else {
console.log("bool");
}
}
)
res.render('viewTask',edit:"readonly",hiding:false});
console.log("viewTask rendered with read-only");
}
});
})
}
})
})
依赖项:
"dependencies": {
"body-parser": "~1.16.0","bytenode": "^1.1.6","cookie-parser": "~1.4.3","debug": "~2.6.0","express": "~4.14.1","express-handlebars": "^5.1.0","express-session": "^1.15.1","express-validator": "^3.2.1","express-zip": "^3.0.0","gridfs-stream": "^1.1.1","hbs": "~4.0.1","js-alert": "^1.0.4","mongoose": "^4.13.21","mongoose-update-if-current": "^1.4.0","morgan": "~1.7.0","multer": "^1.4.2","node-downloader-helper": "^1.0.13","nodemailer": "^3.1.8","passport": "^0.3.2","passport-local": "^1.0.0","popups": "^1.1.3","req-flash": "0.0.3","serve-favicon": "~2.3.2","sweetalert": "^2.1.2"
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)