2

//Here is model

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);

// route

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
}

While all other fields getting saved but the one with array with multiple fields always return blank like "taskinfo : [ ] "

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 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.

Thank You.

11
  • It looks like you have lost some code near line 9 on route. Please check. Commented Sep 13, 2015 at 2:04
  • I am asking logic for saving array as I dont know how to save array with multiple fields, createdon is saved by default and i am not concerned about other fields right now. Commented Sep 13, 2015 at 2:06
  • Does markModified works? Commented Sep 13, 2015 at 2:06
  • i.e. doc.markModified("taskinfo") Commented Sep 13, 2015 at 2:08
  • I am very much new to it, what i learn from all the lecture over net from udemy or pluralsight is for each field type fieldname : res.body.fieldname.. Can you tell me complete logic for multiple field array ? Commented Sep 13, 2015 at 2:10

1 Answer 1

5

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

When you will save task document,

 Var TaskInfo = new TaskInfo({
          isactive:true, 
          taskobject:"", 
          taskdetails:"", 
          iscompleted:true
    })



var task = {};
task.tasktype = req.body.tasktype;

you can push it

 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'
        })

    });
});
Sign up to request clarification or add additional context in comments.

12 Comments

Thanks for a good description sir, but i have read somewhere that embedded document is not required, can you tell me how to achieve this by creating single schema, also can you suggest me some book or course from where i can learn about inserting array field in mongodb using mongoose. stackoverflow.com/questions/15208711/…
please follow this link before design your schema blog.mongodb.org/post/87200945828/…
I have tried your ans, but error popsup, someone said i have to use push to enter & save array.
also Var TaskInfo = new TaskInfo({ isactive:true, taskobject:"", taskdetails:"", iscompleted:true }) here why we arenot taking input from user ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.