3

I've been researching this question to no-ends, but can't find the simple answer I'm looking for. Basically, I'd like to batch POST JSON objects in array.

I've got a a giant array of JSON objects.

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

I'm using an Express server that handles post requests looks like this:

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

These objects are then saved in MongoDB as individual records.

I'm using POSTMAN to send HTTP POSTs to my Express Server. As of now, I've been sending all of my JSON POSTS one at a time, because I can't figure out the best way to batch post all the sub-objects stored in the array as individual objects.

Any suggestions or best practices?

9
  • 1
    An array is JSON too, just send the whole thing, or split up the array into reasonable lengths if you find it too slow. Commented Feb 18, 2016 at 17:28
  • @Adam, right, but I'm using MongoDB on the backend. And I want to consider each JSON object as its own entry. If I send it all as one array object -- I'm sort of facing an issue where I have multiple array objects instead of multiple organization objects. Hope that makes sense? Commented Feb 18, 2016 at 17:30
  • 1
    Then loop over the array on the server. What's the problem? Commented Feb 18, 2016 at 17:31
  • 1
    You are the one who controls what an endpoint expects (array vs object vs both), and how to deal with what you are sent (req.body). You can do whatever you want. Commented Feb 18, 2016 at 17:38
  • 1
    Whats wrong with just inserting array of objects in Mongo? Mongo supports it with a insert statement. docs.mongodb.org/v3.0/reference/method/db.collection.insert/… Commented Feb 18, 2016 at 18:02

2 Answers 2

5

If you send you array as a key in your request body, something like this

enter image description here

You'll get it as req.body.my_restaurants. Then simply use this :

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

I'm assuming that restaurants is the name of your collection.

Sign up to request clarification or add additional context in comments.

5 Comments

Getting YourSchema.insert is not a function. I'm using Mongoose btw. Perhaps thats an issue.
You might not be using Mongoose then. use this db.collection('name_of_your_collection').insert(req.body.my_restaurants, function(err, restaurants){ if(err) console.log(err); else console.log("restaurants Added Successfully"); });
Hey! I got the solution @siddharth. I just needed to change insert to insertMany and it worked. See: mongoosejs.com/docs/api.html#model_Model.insertMany Change your answer and I'll mark yours as correct.
The only downfall of using insertMany is it loses some of my middleware logic that I had on the Mongoose side, but I'll figure out a workaround. Thanks again!
Okay. Just a reminder, in future do specify the Driver and Version of MongoDB you're using as insertMany doesn't work for earlier versions of MongoDB so from what I know insert should also work equally well.
2

I've been playing with the previous solution I marked as correct. An even better approach than using the .insertMany() function is to use the .create() function.

The .insertMany() function skips the middleware associated with .save(), whereas the create() function uses the same process, but can also handle arrays.

So my modified express route looked like the below (where Organization is the name of my schema):

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

And I'm sending JSON objects that look like:

JSON in POSTMAN

Hope this helps someone else down the line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.