Reputation: 9802
I currently have an array
that contains multiple objects
nested inside of it.
Here is the format...
[ { id: 1, title: 'Squats', video: 'https://www.youtube.com/watch?v=aPYCiuiB4PA' }, { id: 2, title: 'Push-Ups',video: 'https://www.youtube.com/watch?v=aPYCiuiB4PA' }]
I am attempting to .save()
it in my Schedule
model that looks like this...
var ScheduleSchema = new mongoose.Schema({
schedule: [Object]
})
When I run the following code in the Schedule
controller on the server side, the .save()
function gives me a 'success' message (that I programmed it to give), but when I look inside my MongoDB
database, it saves incorrectly with nothing inside of the schedule
array.
This is what the saved information looks like in the database....
{ "_id" : ObjectId("56c28a0d4c92bec03408c077"), "schedule" : [ ], "__v" : 0 }
I have a feeling my model is set up wrong.
Model schemas I have tried...
1) schedule: []
2) schedule: [Schema.Types.Mixed]
Just in case this is helpful, here is my .save()
function when I try to save said data from the schedule controller...
var new_schedule = new Schedule(req.body.info);
new_schedule.save(function(err){
if(err){
console.log('err');
}else{
console.log('worked');
}
})
Upvotes: 3
Views: 10873
Reputation: 29
Use this :
var model = mongoose.model("CollectionName",SchemaName);
model.insertMany(arrayOfSchedules , function(err,docs){});
Upvotes: 1
Reputation: 446
I'd suggest setting up your schema like this:
var ScheduleSchema = new mongoose.Schema({
schedule: [mongoose.Schema.Types.Mixed]
});
That would be the proper way to set it up. Another way to do it would be to define the object in-line, like this:
var ScheduleSchema = new mongoose.Schema({
schedule: [{
id: { type: Number, default: 1 },
title: { type: String, default: '', trim: true },
video: { type: String, default: '', trim: true }
}]
});
Also, it may not be a problem on the server side code. Before you create the new Schedule with req.body.info, you might want to put in a line like this:
console.log(JSON.stringify(req.body, null, 2));
This will print out the req.body object in a readable format so that you can make sure the client app is sending the information you think you are getting.
Upvotes: 9