简体   繁体   English

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

[英]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. 我想以以下方式存储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" } ] } ] } 

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)? 但是我不知道如何从前端(html / js)发送数据?

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. 尽管某些数据应由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 现在我正在使用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"); }); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM