How to perform a bulk insert via sails-mongo and sails.js?
Asked Answered
T

1

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?

Trimetric answered 1/7, 2014 at 10:32 Comment(1)
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 mongodbTrimetric
C
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

Cleft answered 1/7, 2014 at 12:34 Comment(3)
Does this only work in the beta version, or will it work in stable version (0.9.x)?Trimetric
This looks great. Will it also work for Model.findOrCreate() ? If so what would be the syntax? ThanksSupper
@KevinBaker: Yes - see: sailsjs.org/#/documentation/reference/waterline/models/…. You can use several records or criteriaCleft

© 2022 - 2024 — McMap. All rights reserved.