How to save an array of objects to mongoose DB with only one call?


Question

Is there any way to save an array of JSON object to a mongodb with only one call? something like:

schemeObject.save(array_of_json_object, callback);

I'm using mongoosejs

1
17
4/3/2012 5:01:01 AM

Accepted Answer

I do not think its possible with mongooosejs. You can however use BATCH insert of mongodb ,which is supported natively.

Helpful links:

http://www.mongodb.org/display/DOCS/Inserting#Inserting-Bulkinserts

https://groups.google.com/forum/#!msg/mongoose-orm/IkPmvcd0kds/bZuYCN_orckJ

9
4/3/2012 5:35:03 AM

There is a way to batch insert with MongooseJS. I'm not sure if it's a new feature since this question was asked/answered, but I figured if someone were to come here from a search, they should know the way to do it.

var array = [{ type: 'jelly bean' }, { type: 'snickers' }];
Candy.create(array, function (err, jellybean, snickers) {
  if (err) // ...
});

Here are the docs: http://mongoosejs.com/docs/api.html#model_Model.create


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon