如何将 JSON 对象数组发布到 mongodb?
Posted
技术标签:
【中文标题】如何将 JSON 对象数组发布到 mongodb?【英文标题】:How to post a JSON object array onto mongodb? 【发布时间】:2020-07-19 03:48:57 【问题描述】:我正在尝试创建一个锻炼管理应用程序,它允许教练 (userType = true) 为用户创建锻炼。不幸的是,我不确定如何格式化我的 mongodb 模式以接收练习数量。我的想法是创建一个 JSON 对象数组,然后将其推送到数据库中,以便稍后显示给用户。问题是我不知道如何正确创建允许正确解析数据的表单。我决定创建几个表单,然后使用 JQuery 中的 .serializeObject() 方法将它们转换为 JSON 对象,然后将它们推送到一个数组中。但是我不知道如何将该数组推送到 mongoDB
这是我的gym.hbs文件
<div id ="createWorkout" class="hiddenWorkout workoutCreator">
<form id="exerciseForm" class="gymFormPadding" action="/dashboard/gym" method="POST">
<div id="exerciseInput" class="form-group" style="display:flex;width:100%">
<input id="exerciseName" class="form-control" type="text" name="name" placeholder="Exercise Name" value=""/>
<input id="exerciseSets" class="form-control" type="number" name="sets" placeholder="Sets" value=""/>
<input id="exerciseReps" class="form-control" type="number" name="reps" placeholder="Reps" value=""/>
<input id="exerciseWeight" class="form-control" type="number" name="weight" placeholder="Weight" value="" />
</div>
</form>
<div id="newExerciseAdd"></div>
<input type="hidden" name="_csrf" value="cybersecurity">
<button type="button" class="btn btn-primary" id="addExerciseAdd">Add New Exercise</button>
<button id="submitWorkout" type="submit" class="btn btn-primary" name="upload">Submit Workout</button>
</div>
这是负责操作的 JQuery
var counter = 1;
$(document).ready(function()
$("#createWorkout").click(function()
$(".hiddenWorkout").removeClass('hiddenWorkout');
$("#createWorkout").hide();
);
);
$(document).ready(function()
var i = 0;
$("#addExerciseAdd").click(function()
if(i<9)
$("#exerciseForm").clone().appendTo("#newExerciseAdd");
i++;
$("#newExerciseAdd #exerciseForm").attr("id","exerciseForm"+i);
$("#newExerciseAdd #exerciseInput").attr("id","exerciseInput"+i);
$("#newExerciseAdd #exerciseName").attr("id","exerciseName"+i);
$("#newExerciseAdd #exerciseSets").attr("id","exerciseSets"+i);
$("#newExerciseAdd #exerciseReps").attr("id","exerciseReps"+i);
$("#newExerciseAdd #exerciseWeight").attr("id","exerciseWeight"+i);
counter++;
);
);
$(document).ready(function()
var array = [];
var object;
var exerciseNo = exerciseNo;
$("#submitWorkout").click(function()
$("#exerciseForm").attr("id","exerciseForm0");
$("#exerciseInput").attr("id","exerciseInput0");
$("#exerciseName").attr("id","exerciseName0");
$("#exerciseSets").attr("id","exerciseSets0");
$("#exerciseReps").attr("id","exerciseReps0");
$("#exerciseWeight").attr("id","exerciseWeight0");
for(var i=0;i<counter;i++)
object=$("#exerciseForm"+i).serializeObject();
array.push(object);
);
console.log(array);
);
这是我的 controller.js
const mongoose = require('mongoose');
const Gym = require('../models/gym');
exports.getGymPage = async (req, res) =>
let coachStatus = req.user.userType;
let teamID = null;
if(res.locals.team)teamID = res.locals.team._id
let exercises = await Gym.find(teamId:teamId);
res.render('/dashboard/gym.hbs',title: "Gym",exercises : exercises, coachStatus : coachStatus, cybersecurity: `$req.csrfToken()`);
;
exports.addExercise = (req, res) =>
const exercise = new Gym(teamID:res.locals.team._id, name: req.body.exerciseName,sets:req.body.exerciseSets,reps:req.body.exerciseReps,weight:req.body.exerciseWeight,cybersecurity: `$req.csrfToken()`).save();
res.redirect('/dashboard/gym');
这是我的models.js
const mongoose = require('mongoose');
const Schema = mongoose;
const gymSchema = new Schema(
teamID :
type: mongoose.Schema.ObjectId
,
exercises:[
name : type:String, require: true,
sets : [],
reps : type: Number, require: true,
weight : type: Number,,
]
);
module.exports = mongoose.model('Gym', gymSchema);
我推送文件的方法完全荒谬吗?我觉得我在转圈,却无处可去。有更好的方法吗? 请原谅我最可能的可怕代码,我才刚刚开始。 谢谢!
【问题讨论】:
【参考方案1】:MongoDB 应该知道它是一个数据数组。您是否尝试过通过这样做来访问练习,例如...:
const exercise = req.body.exercise[0].name;
const exerciseData = JSON.parse(exercise);
然后推送,你可以使用常规的 array.push 我不完全了解您要做什么,但是希望这可以有所帮助...
【讨论】:
以上是关于如何将 JSON 对象数组发布到 mongodb?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 MongoDB 的嵌入式数组对象中插入 json 对象?
如何在mongodb的单个json文档中过滤对象数组? [复制]
如何通过 JSON 对象将数据保存到 mongodb 数据库?