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


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

I'm using mongoosejs

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:!msg/mongoose-orm/IkPmvcd0kds/bZuYCN_orckJ

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:

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