如何使用mongoose以Mongodb嵌套模式的形式保存数据

时间:2017-05-05 17:38:27

标签: javascript node.js mongodb mongoose

我想以这样的方式构建mongodb,以便以下列方式存储数据。



{
	"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"
				}
			]
		}
	]
}




到目前为止,我在搜索之后对结构做了什么



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




现在,如果我使用以下代码保存硬编码数据,那么它可以正常工作



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




但是我无法弄清楚我应该如何从前端发送数据(html / js)?

1 个答案:

答案 0 :(得分:0)

得到解决方案:

首先将以下代码放在控制器文件

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

现在使用前端传递数据

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

虽然某些数据应由nodejs处理。我只是为了简单起见而在这里列出。

现在我使用ajax将数据从前端发送到节点

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