Rakesh Soni
Rakesh Soni

Reputation: 1333

How to save data in the form of Mongodb Nested schema using mongoose

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)?

Upvotes: 0

Views: 436

Answers (1)

Rakesh Soni
Rakesh Soni

Reputation: 1333

Got solution :

First put the following code in controller file

	var reqBody = req.body;
	var choices = reqBody.choices;
	var choicesnew = [];
	for (var i = choices.length - 1; i >= 0; i--) {
		var votes = [];
		var choice = {text:choices[i],votes: votes};
		choicesnew.push(choice);
	}
	var newPoll = {
		question: reqBody.question,
		choices: choicesnew
	}
	var poll = new Poll(newPoll);
	poll.save(function(err, data){
		res.json(reqBody);
	});

And now pass the data using front end

<form action="#">
		<input type="text" name="question" placeholder="question">
		<input type="text" name="choices" placeholder="choices">
		<input type="text" name="choices" placeholder="choices">
		<input type="text" name="choices" placeholder="choices">
		<input type="text" name="ip" placeholder="ip">
		<input type="submit">
	</form>

Although some data should be processed by nodejs. I am just listing here just for the sake of simplicity.

Now i am using ajax to send data from front end to node

$("form").on('submit', function(event) {
			event.preventDefault();
			$.ajax({
				url: '/new',
				type: 'POST',
				data: $(this).serializeArray(),
			})
			.done(function(data) {
				console.log(data);
			})
			.fail(function() {
				console.log("error");
			})
			.always(function() {
				console.log("complete");
			});
			
		});

Upvotes: 1

Related Questions