gurudatta kr
gurudatta kr

Reputation: 27

how to insert multiple JSON objects to one property of a schema?

I have a requirement to store multiple JSON objects in a property of schema. take this example...

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

     const Student= Schema({
        student_id: String,
        name:String
        attendance:[
                  { 
                 date: Date,
                 Status: String
                   }
                ]
        });

I need to insert attendance of individual student which looks like this..

       student_id: student_001,
        name:'Joe'
        attendance:[
                     { 
                      date: 24-10-2018,
                      status: 'Present'
                     },
                     { 
                      date: 25-10-2018,
                      status: 'Absent'
                     },
                  //list goes on
                   ]

I am using NodeJs as Backend, EJS template as front end and mongodb database. Date and Status comes when user submits data from front end. So I am having hard time writing my post request. Any types of comments / suggestions / change of model structure are welcome. Thank you.

Upvotes: 0

Views: 1884

Answers (2)

Vishal-L
Vishal-L

Reputation: 1347

You can create a separate attendance Schema.

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

const AttendanceSchema = new Schema({ 
                             date: Date,
                             status: String
                         });

const StudentSchema = new Schema({
    student_id: String,
    name:String
    attendance:[AttendanceSchema]
});

const Student = mongoose.model('Student', StudentSchema);

Add a new Student.

let newStudent = new Student({
        student_id: student_001,
        name:'Joe'
});
newStudent.save();

Update attendance:

let att1 =  { 
               date: 24-10-2018,
               status: 'Present'
           };

// Here 'id' is id of particular student.
Student.update({ _id: id }, { $push: { attendance: att1 } })
    .then(() => console.log("Success"))
    .catch(err => console.log(err));

At some point later:

let att2 =   { 
                  date: 25-10-2018,
                  status: 'Absent'
             };

// Here 'id' is id of particular student.
Student.update({ _id: id }, { $push: { attendance: att2 } })
    .then(() => console.log("Success"))
    .catch(err => console.log(err));

Upvotes: 1

ykit9
ykit9

Reputation: 493

I suggest you to change the model structure to be normalized. This will improve your experience in future statistics querying.

Also, one more suggestion - do not use string indentifiers in mongoDB, this can cause a headache in maintaining their uniqueness. Mongo has automated _id property assigning to each document, you could use it if you need to indentify any object.

Considering my suggestions - the code will look like:

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

const Student = Schema({
    name: String
});

const Attendance = Schema({
    date: Date,
    status: String,
    student_id: {
        type: Schema.Types.ObjectId,
        ref: 'Student'
    }
})

Then, you could simply create attendance records assigned to the student :

const attendance = new AttendanceModel({
    date: new Date('05/20/2018'),
    status: "present",
    student_id: "somestudentid"
});

Upvotes: 1

Related Questions