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
One of the benefits of MongoDB's rich schema model is the ability to store arrays as document field values. Storing arrays as field values allows you to model one-to-many or many-to-many relationships in a single document, instead of across separate collections as you might in a relational database.
Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.
create() is a shortcut for saving one or more documents to the database. MyModel. create(docs) does new MyModel(doc). save() for every doc in docs.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With