Post Multiple JSON Objects Simultaneously with Express and Postman
Solution 1
If you send you array as a key in your request body, something like this
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.
Solution 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:
Hope this helps someone else down the line.
Ahmed Haque
Updated on February 19, 2020Comments
-
Ahmed Haque about 4 years
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?
-
Ahmed Haque about 8 yearsGetting YourSchema.insert is not a function. I'm using Mongoose btw. Perhaps thats an issue.
-
SiddAjmera about 8 yearsYou 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"); });
-
Ahmed Haque about 8 yearsHey! I got the solution @siddharth. I just needed to change
insert
toinsertMany
and it worked. See: mongoosejs.com/docs/api.html#model_Model.insertMany Change your answer and I'll mark yours as correct. -
Ahmed Haque about 8 yearsThe 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!
-
SiddAjmera about 8 yearsOkay. 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.