MongoDB请求主体问题 [英] MongoDB req.body Issue
本文介绍了MongoDB请求主体问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非常简单的Mongo设置,如下所示.这非常适合从输入字段获取数据并保存数据.一切正常.
I have a very simple Mongo set up as shown below. This works perfectly to get data from an input field and save the data. All working.
我的问题:我将如何遍历前端的jobs变量并设置数据,以便它可以与我的模型一起使用.我需要以某种方式将其输入,以便我可以请求正文吗?
数据:
var jobs = [
{
name: "Accountant"
salary: 0,
}, {
name: "Actor"
salary: 0,
}, {
name: "Actuary"
salary: 0
}]... + hundreds more...
我的Mongo模式:
var JobSchema = new mongoose.Schema({
name: String,
salary: Number
});
module.exports = mongoose.model('jobs' , jobSchema)
我的发布路线:
router.post('/register', function(req, res) {
var job = ({
name: req.body.name,
salary: req.body.salary,
})
要发布的表单:
<form action="/register" method="post">
<textarea class='jobnames' type="text" name="name" placeholder="name"> </textarea>
<textarea class='2' type="number" name="salary" placeholder="salary"> </textarea>
<button >Submit</button>
</form>
推荐答案
您可以使用insertMany()
查询进行尝试.
You can try it using the insertMany()
query .
req.body = [
{
name: "Accountant",
salary: 0,
},
{
name: "Actor",
salary: 0,
},
{
name: "Actuary",
salary: 0
}
]
db.collection.insertMany(req.body);
借助此查询,您可以一次插入多个文档.
唯一ID,即_id将自动生成.
With the help of this query , you can insert multiple documents at a time .
unique id i.e., _id will be automatically generated .
有关insertMany()
的更多信息,请访问官方文件
For more information about insertMany()
visit the official document
这篇关于MongoDB请求主体问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文