我要保存使用下面的架构在数据库中的数据:创建路线
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);
这里是链接到JSON文件:Link
[
{
"_id": "55f4e4f5fe8b36980a611519",
"tasktype": "Basic",
"__v": 0,
"taskinfo": [
{
"isactive":"True" ,
"taskobject": "paid",
"taskdetails": "This is task one",
"iscompleted": "False"
},
{
"isactive": "False",
"taskobject": "free",
"taskdetails": "This is task two",
"iscompleted": "False"
},
{
"isactive": "True",
"taskobject": "paid",
"taskdetails": "This is task three",
"iscompleted": "False"
},
{
"isactive": "True",
"taskobject": "free",
"taskdetails": "This is task four",
"iscompleted": "False"
}
],
"visitedby": [],
"createdon": "2015-09-13T02:52:37.512Z"
}
]
我想写路线(控制器)在nodejs使用rest api,但我不知道如何使用猫鼬在mongodb中保存数组字段。
如果你可以请建议我很好的资源,因为我买了一些udemy课程,但有导师只告诉fieldname:res.body.fieldname采取用户输入 这是不适合我在这种情况下工作。
这将是巨大的,如果你能告诉我怎样才能救阵在我的数据库,
下面是示例路径文件,这是不正确,请帮我
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
}
Package.json [on request]
{
"name": "todotask",
"version": "1.0.0",
"description": "test module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "madhur",
"license": "ISC",
"dependencies": {
"body-parser": "*",
"express": "^4.13.3",
"express-session": "*",
"gulp": "^3.9.0",
"gulp-nodemon": "^2.0.3",
"mongoose": "*",
"morgan": "*",
"multer": "^1.0.3",
"passport": "*",
"passport-facebook": "*",
"passport-google": "*",
"passport-http": "*",
"passport-local": "*",
"passport-twitter": "*",
"serve-favicon": "*",
"socket.io": "*"
}
}
谢谢
你可以发送数组在req.body – abhaygarg12493
它不工作abhay,有人告诉我你必须推 –
推是在mongoDB查询,但在路由,你可以简单地发送数据列表 – abhaygarg12493