0

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

4
  • you can simply do let poll = new require("yourSchema,js"); and then poll.save(callback); Commented May 5, 2017 at 17:52
  • Actually i am able to figure out how . My code is working fine with hard coded data Commented May 5, 2017 at 17:54
  • well, whats your question though? Commented May 5, 2017 at 17:56
  • Updated question, if that can help Commented May 5, 2017 at 18:00

1 Answer 1

1

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");
			});
			
		});

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

Comments

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.