1

So, I'm sending data from angular reactive form like: Angular reactive form UI image

and Data being sent to backend in browser console image

I have made schema for task as:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let taskSchema = new Schema({
    taskId:{
        type:String,
        unique:true
    },
    task:{
        type:String,
        default:''
    },
    subTask:[{ 
        type: Schema.Types.ObjectId, 
        ref: 'SubTask'
    }]
}
module.exports = mongoose.model('Task',taskSchema);

Schema for Sub-Task as:

let subTaskSchema = new  Schema({
    title:String,
    subSubTask:[{ 
        type: Schema.Types.ObjectId, 
        ref: 'SubSubTask'
    }]
})
module.exports = mongoose.model('SubTask',subTaskSchema);

Schema for Sub-Sub-Task as:

let subSubTaskSchema = new  Schema({
    title:String
})
module.exports = mongoose.model('SubSubTask',subSubTaskSchema);

Now,I'm confused about how to save nested array of objects data in mongodb using mongoose?

7
  • i think you made error in this line not sure of it but please check module.exports = mongoose.model('subTask',subTaskSchema); it should be module.exports = mongoose.model('SubTask',subTaskSchema); when you are using it to refer you using 'SubTask' Commented Feb 22, 2019 at 12:48
  • fixed this one, I'm having trouble in polpulating it, its giving me empty subTask array Commented Feb 22, 2019 at 12:51
  • can you share the code via git or something? Commented Feb 22, 2019 at 13:11
  • github.com/sanjayadav/ToDoList/tree/master/app/controllers , open dashboardController and code is in createTask() -> taskCreationFunction() & subTaskCreationFunction() Commented Feb 22, 2019 at 13:31
  • 1
    Thanks for the help, actually the problem was in data which was sent by angular, I had to stringify data before sending and parsed it after receiving on backend, then proceeded with steps above and it worked. Commented Feb 24, 2019 at 7:32

2 Answers 2

1

you can define your schema like this

const userSchema = new mongoose.Schema({
    likedBooks: [{
        type: mongoose.Types.ObjectId,
        ref: 'books'
    }],
    email: {
        type: String,
        required: true
    },
    name: {
        type: String,
        required: true
    }
});

exports.User = mongoose.model('users', userSchema);

then you can populate data by doing

user = User.find({ email: req.body.email }).populate('likedBooks');

here likedBooks contains _id of each book

const bookSchema = new mongoose.Schema({
    isbn: {
        type: Number,
        required: true
    },
    name: {
        type: String,
        required: true
    },
    author: {
        type: String,
        required: true
    },
    publisher: {
        type: String,
        default: ""
    },
    imageUrl: {
        type: String,
        required: true
    },
    description: {
        type: String,
        default: ""
    }
});

exports.Book = mongoose.model('books', bookSchema);

for both schema i have not put _id as it is auto generated by mongodb and it is used as reference

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

5 Comments

Hey, could you please share books schema as well?
@SanjayYadav i have eddited my answer please check
Thanks for sharing the schema, I've updated my schema, now there are 3 schema in total for my data, I've edited the question, please check if schema are correct.
Also, please help me in proceeding to next step, I'm stuck on this issue from last 2 days.
all the documents will have _id which will be auto generated by mongo db, store this _id of subTask in subtask array of task
0

The Object Model should look like this before saving.

{ taskId: 1, task: 'Do something', subTask: [{ title: 'Write a essay', subSubTask: [{ title: 'Collect details to write an essay' }] }] }

Hope it helps...

5 Comments

Thanks for replying but actually it's not what I'm looking for.
I'm sending all the data from angular service through parameters, so I'm using req.body for eg. { task : req.body.task } but how do I do it for array of objects in my question above?
Are you facing problem in sending the values to server? Or is there any problem while you are trying to save the value using mongoose?
There's an error when I'm trying to save values using mongoose, How do I save nested array of objects using mongoose?
You don't need to do anything specific just a usual save. Can you provide your here? So we can analyse it quickly.

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.