Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:51:11+00:00 2026-06-05T06:51:11+00:00

I am trying to save a task to a list of tasks with mongoose

  • 0

I am trying to save a task to a list of tasks with mongoose and MongoDB. I want to save it redundantly in the tasks collection and in the corresponding list document as embedded document.

It works fine but one little thing: The list’s embedded documents don’t have their objectIds. But I need them in order to connect them logically with the documents in the tasks-collection.

My Schemas:

var TaskSchema = new Schema({
    _id: ObjectId,
    title: String,
    list: ObjectId

});

var Task = mongoose.model('task', TaskSchema);

var ListSchema = new Schema({
    _id: ObjectId,
    title: String,
    tasks: [Task.schema]
});

var List = mongoose.model('list', ListSchema);

My controller/router:

app.post('/lists/:list_id/tasks', function(req, res) {

        var listId = req.params.list_id;   

        // 1. Save the new task into the tasks-collection.

        var newTask = new Task();
        newTask.title = req.body.title;
        newTask.list = listId; 

        console.log('TaskId:' + newTask._id);  // Returns undefined on the console!!!

        newTask.save(); // Works fine!

        // 2. Add the new task to the coresponding list.

        list.findById(listId, function(err, doc){

            doc.tasks.push(newTask);

            doc.save();  // Saves the new task in the list but WITHOUT its objectId

            });

        res.redirect('/lists/' + listId)

});

Can I use mongoose a different way to achieve that? Or do I have to save the task and then query it before saving it in the list?

Thank you for advise 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:51:12+00:00Added an answer on June 5, 2026 at 6:51 am

    I solved it by using an awesome feature called populate!

    Also dtryon was right: You don’t need to declare your _id ObjectIds in your models, they are getting added anyways. And you have to nest this stuff because your task is being saved asyncronously so you must get sure that runs before the other stuff.

    Here is the solution:

    Schemas:

    var TaskSchema = new Schema({
        title: String,
        list: { type: Schema.ObjectId, ref: 'list' }
    
    });
    
    var Task = mongoose.model('task', TaskSchema);
    
    var ListSchema = new Schema({
        title: String,
        tasks: [{ type: Schema.ObjectId, ref: 'task' }]
    });
    
    var List = mongoose.model('list', ListSchema);
    

    Controller/router:

    app.post('/lists/:list_id/tasks', function(req, res) {
    
        var listId = req.params.list_id;   
    
        var newTask = new Task();
        newTask.title = req.body.title;
        newTask.list = listId; 
    
    
        // WHEN SAVING, WRAP THE REST OF THE CODE
    
        newTask.save(function (err){
           if (err) {
                console.log('error saving new task');
                console.log(err);
            } else {
                console.log('new task saved successfully'); 
    
                list.findById(listId), function(err, doc){
    
                    doc.tasks.push(newTask);
    
                    doc.save(function (err){
                        if (err) {
                        console.log('error adding new task to list');
                        console.log(err);
                        } else {
    
                        console.log('new task saved successfully'); 
                        res.redirect('/lists/' + listId);
    
                        }  
                    });
                });
            });
        });
    });
    

    Now it references correctly and with the populate feature you can access the entries very comfortably. Works like charm 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to save a row in my settings table, it works all fine
I've been trying to build a task list in Android, and I want it
I am trying to save changes to my database trough a rake task. In
Im trying to save a simple script in netbeans to htdocs, but i only
When trying to save the database using boost serialization, I encounter the segfault that
I'm trying to save some data into an array but unfortunately all the data
I'm trying to save a CUploadedFile object in local folder, I am using img
I'm trying to save my Photo class that has a byte[] File field. When
I'm trying to save a huge NSArray to a file and then retrieve it...
I am trying to save a simple template to pdf using the rendering plugin,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.