使用Express和Postman同时发布多个JSON对象

使用Express和Postman同时发布多个JSON对象,json,node.js,mongodb,post,express,Json,Node.js,Mongodb,Post,Express,我一直在研究这个问题,但找不到我想要的简单答案。基本上,我希望在数组中批处理POST JSON对象 我有一个巨大的JSON对象数组 [ { "Name": "SEARCH Resource Center", "Address": "2505 Fannin St, Houston, TX 77002", "Phone": "(713) 739-7752", "Hours": "Mon-Fri, 8am to 3pm", "Category": "Drop-

我一直在研究这个问题,但找不到我想要的简单答案。基本上,我希望在数组中批处理POST JSON对象

我有一个巨大的JSON对象数组

[ 
 {
   "Name": "SEARCH Resource Center",
    "Address": "2505 Fannin St, Houston, TX 77002",
    "Phone": "(713) 739-7752",
    "Hours": "Mon-Fri, 8am to 3pm",
    "Category": "Drop-In Centers"
 },
 {
   "Name": "Salvation Army Social Services - Young Adult Resource Center",
   "Address": "2208 Main St, Houston, TX 77002",
   "Phone": "(713) 658-9205",
   "Hours": "Mon-Thurs, 11am to 3pm",
   "Category": "Drop-In Centers"
 },
 ...
]
我使用的Express服务器处理post请求,如下所示:

app.post('/api/orgs', function(req, res) {

  // Creates a new User based on the Mongoose schema and the post body
  var newOrg = new Organization(req.body);

  // New User is saved in the db.
  newOrg.save(function(err){
    if(err)
      res.send(err);

    // If no errors are found, it responds with a JSON of the new user
    res.json(req.body);
  });
});
然后,这些对象作为单个记录保存在MongoDB中

我正在使用POSTMAN向我的Express服务器发送HTTP帖子。到目前为止,我一直在一次一个地发送我所有的JSON帖子,因为我无法找到将数组中存储的所有子对象作为单个对象批发布的最佳方法


有什么建议或最佳实践吗

如果将数组作为请求正文中的键发送,则如下所示

你可以在我的餐厅里拿到。然后简单地使用这个:

db.collection('restaurants').insertMany(req.body.my_restaurants, function(err, restaurants){
    if(err) console.log(err);
    else console.log("restaurants Added Successfully");
});

我假设餐厅是您收藏的名称

我一直在使用之前标记为正确的解决方案。比使用
.insertMany()
函数更好的方法是使用
.create()
函数

.insertMany()
函数跳过与
.save()
关联的中间件,而
create()
函数使用相同的过程,但也可以处理数组

因此,我修改后的快速路线如下所示(其中,
Organization
是我模式的名称):

我发送的JSON对象如下所示:


希望这对其他人有所帮助。

一个数组也是
JSON
,只需发送整个内容,或者如果您觉得数组太慢,可以将其拆分为合理的长度。@Adam,是的,但我在后端使用MongoDB。我想把每个JSON对象看作它自己的条目。如果我将其全部作为一个数组对象发送--我会面临这样一个问题:我有多个数组对象,而不是多个组织对象。希望这有意义?然后在服务器上的阵列上循环。问题是什么?您是控制端点期望的内容(
array
vs
object
vs两者)以及如何处理发送的内容(
req.body
)的人。你可以做你想做的任何事情。在Mongo中插入对象数组有什么错?Mongo支持insert语句。获取schema.insert不是一个函数。顺便说一句,我用的是猫鼬。也许这是个问题。那时你可能没有用猫鼬。使用此
db.collection('name_of_your_collection')。插入(req.body.my_restaurants,function(err,restaurants){if(err)console.log(err);else console.log(“成功添加了餐厅”);}insert
更改为
insertMany
,就可以了。请看:更改您的答案,我会将您的答案标记为正确。使用insertMany的唯一缺点是它丢失了我在Mongoose方面的一些中间件逻辑,但我会找到一个解决方法。再次感谢!可以提醒一下,将来一定要指定您作为insertMany使用的MongoDB的驱动程序和版本,因为它不适用于早期版本的MongoDB,所以据我所知,insert也应该同样适用。
app.post('/api/orgs', function(req, res) {

// Array of JSON Objects
if (req.body.batch){
  Organization.create(req.body.batch, function(err){
    if(err)
      res.send(err);

    else
      res.json(req.body);
  });
}
// Single JSON Object
else {
  var newOrg = new Organization(req.body);

  // New User is saved in the db.
  newOrg.save(function(err){
    if(err)
      res.send(err);

    // If no errors are found, it responds with a JSON of the new user
    else
      res.json(req.body);
  });
}
});