问题描述
//这里是模型
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
// Task schema
var taskSchema = mongoose.Schema({
tasktype : {type: String},
createdon : {type: Date, default: Date.now},
createdby : {type: Schema.Types.ObjectId,ref: 'User'},
visitedby : [{type: Schema.Types.ObjectId,ref: 'User'}],
taskinfo : [{ isactive:Boolean, taskobject:String, taskdetails:String, iscompleted:Boolean}]
});
module.exports = mongoose.model('Task', taskSchema);
//路线
var Task = require ('../models/task');
var User = require ('../models/user');
var config = require ('../../config');
module.exports = function(app, express) {
var api = express.Router();
api.post('/tasks', function (req, res) {
var task = new Task({
// ...
tasktype : req.body.tasktype,
taskinfo : req.body.taskinfo,
});
task.save(function(err){
if(err){
res.send(err);
return;
}
res.json({message:'Task has been created'})
});
return api
}
虽然得到保存的所有其他领域,但一个与多个领域的数组总是返回空白,如taskinfo:[]
While all other fields getting saved but the one with array with multiple fields always return blank like "taskinfo : [ ] "
POST方法REST API来发布任务分解成猫鼬数据库,阵列单场一切工作正常,但多场阵列是没有得到保存,有人请帮助我在这里。
The post method is REST API to post a task into mongoose database, for array with single field everything working fine but array with multiple field is not getting saved, someone please help me here.
Basic的帮助,将被罚款,只是请教教我如何保存多场阵。
Basic help will be fine, just please teach me how to save "multiple field array".
Mongoose doesnot always require subdocument structure and this can be achieved by the above model, please dont advice to use subdocument structure, I want to learn this.
感谢你。
推荐答案
我觉得如果taskinfo有多个值,并且希望将其保存为任务书里面嵌入文档。你应该有任务的信息不同的文档。所以,你可以节省像
I think if taskinfo has a multiple values and you want to save it as embedded document inside task document. You should have different document of task info. So,you can save like that
var TaskInfoSchema = require("taskInfo.js").TaskInfoSchema
var taskSchema = mongoose.Schema({
tasktype : {type: String},
createdon : {type: Date, default: Date.now},
createdby : {type: Schema.Types.ObjectId,ref: 'User'},
visitedby : [{type: Schema.Types.ObjectId,ref: 'User'}],
taskinfo : [TaskInfoSchema]
});
module.exports = mongoose.model('Task', taskSchema);
现在你将有不同的文档像
And now you will have different document as task info like
var taskInfo = mongoose.Schema({
isactive:{type:Boolean},
taskobject:{type:String},
taskdetails:{type:String},
iscompleted:{type:Boolean}
});
var TaskInfo = mongoose.model('TaskInfo', taskSchema);
module.exports.TaskInfo = TaskInfo
module.exports.TaskInfoSchema = taskSchema
当你将节省任务书,
Var TaskInfo = new TaskInfo({
isactive:true,
taskobject:"",
taskdetails:"",
iscompleted:true
})
var task = {};
task.tasktype = req.body.tasktype;
你可以把它
task.taskinfo = [];
for (var i = 0; i < req.body.taskInfo.length; i++) {
var taskInfo = new TaskInfo(req.body.taskInfo[i]);
task.taskinfo.push(taskInfo);
}
然后,你将保存任务书
Then you will save task document
var taskObj = new Task(task);
taskObj.save(function (err) {
if (err) {
res.send(err);
return;
}
res.json({
message: 'Task has been created'
})
});
});
这篇关于猫鼬使用保存多个领域数组中的MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!