10

Is there a way to perform a bulk insert via sails-mongo. So if I pass an array of documents to the model's create method, it will insert each document within that array atomically into the database?

ses
  • 158
  • 1
  • 7
  • I have tried passing the array directly into Model.create() (with Model being the name of my given model) and I have also tried Model.createEach()... it just indexes it as an actual array, rather than as each document in the array as you would expect from a batch insert with mongodb – ses Jul 01 '14 at 12:12

1 Answers1

12

It works exactly like you asked:

Model.create([{foo: "first},{foo:"2nd"}]).exec(...

See http://beta.sailsjs.org/#/documentation/reference/Models/Model-Methods/create.html

mdunisch
  • 3,627
  • 5
  • 25
  • 41
  • Does this only work in the beta version, or will it work in stable version (0.9.x)? – ses Jul 01 '14 at 13:47
  • This looks great. Will it also work for Model.findOrCreate() ? If so what would be the syntax? Thanks – Kevin Baker Jan 12 '15 at 19:24
  • @KevinBaker: Yes - see: http://sailsjs.org/#/documentation/reference/waterline/models/findOrCreate.html. You can use several records or criteria – mdunisch Jan 13 '15 at 07:57