I want to structure mongodb in such a way that it store data in following way.
{
"question" : "Was today's decision right?",
"choices" : [
{
"text" : "yes",
"votes" : [
{
"ip" : "123.123.123.123",
"time" : "123444"
}
]
},
{
"text" : "no",
"votes" : [
{
"ip" : "123.123.123.123",
"time" : "123444"
},
{
"ip" : "123.123.123.123",
"time" : "123444"
},
{
"ip" : "123.123.123.123",
"time" : "123444"
}
]
}
]
},
{
"question" : "Was yesterday's decision right?",
"choices" : [
{
"text" : "yes",
"votes" : [
{
"ip" : "123.123.123.123",
"time" : "123444"
}
]
},
{
"text" : "no",
"votes" : [
{
"ip" : "123.123.123.123",
"time" : "123444"
},
{
"ip" : "123.123.123.123",
"time" : "123444"
},
{
"ip" : "123.123.123.123",
"time" : "123444"
}
]
}
]
}
What i have done so far for structure after little searching
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var voteSchema = new Schema({
ip: String
});
var choiceSchema = new Schema({
text: String,
votes: [voteSchema]
});
var PollSchema = new Schema({
question: { type: String, required: true },
choices: [choiceSchema]
});
module.exports = mongoose.model('Polls', PollSchema);
Now if I use following code to save hard coded data then it's working fine
var poll = new Poll({
question : reqBody.question,
choices : [
{
text : "yes",
votes : [
{
ip : "123.123.123.123"
}
]
},
{
text : "no",
votes : [
{
ip : "123.123.123.123",
},
{
ip : "123.123.123.123",
},
{
ip : "123.123.123.123",
}
]
}
]
});
poll.save(function(err, data) {
res.json(data);
});
But i am not able to figure how i should send data from front end (html/js)?