I know in the latest version of Mongoose you can pass multiple documents to the create method, or even better in my case an array of documents.
var array = [{ type: 'jelly bean' }, { type: 'snickers' }];
Candy.create(array, function (err, jellybean, snickers) {
if (err) // ...
});
My problem is that the size of the array is dynamic so in the callback it would be helpful to have an array of the created objects.
var array = [{ type: 'jelly bean' }, { type: 'snickers' }, ..... {type: 'N candie'}];
Candy.create(array, function (err, candies) {
if (err) // ...
candies.forEach(function(candy) {
// do some stuff with candies
});
});
Not in the documentation, but is something like this possible?
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.
Mongoose never create any collection until you will save/create any document.
Mongoose models have a create() function that is often used to create new documents. const User = mongoose.model('User', mongoose.Schema({ email: String })); const doc = await User.create({ email: '[email protected]' }); doc instanceof User; // true doc.email; // '[email protected]'
Mongoose models have a create () function that is often used to create new documents. The create () function is a thin wrapper around the save () function . The above create () call is equivalent to:
Mongoose – Insert Multiple Documents to MongoDB. To insert Multiple Documents to MongoDB using Mongoose, use Model.collection.insert(docs_array, options, callback_function); method. Callback function has error and inserted_documents as arguments. Model.collection.insert(docs, options, callback)
Every model has an associated connection. When you use mongoose.model (), your model will use the default mongoose connection. If you create a custom connection, use that connection's model () function instead. Finding documents is easy with Mongoose, which supports the rich query syntax of MongoDB.
Since Mongoose v5 you can use insertMany According to the mongoose site it is faster than .create (): Shortcut for validating an array of documents and inserting them into MongoDB if they're all valid. This function is faster than .create () because it only sends one operation to the server, rather than one for each document.
You can access the variable list of parameters to your callback via arguments
. So you could do something like:
Candy.create(array, function (err) {
if (err) // ...
for (var i=1; i<arguments.length; ++i) {
var candy = arguments[i];
// do some stuff with candy
}
});
With Mongoose v5.1.5, we can use insertMany() method with array passed.
const array = [
{firstName: "Jelly", lastName: "Bean"},
{firstName: "John", lastName: "Doe"}
];
Model.insertMany(array)
.then(function (docs) {
response.json(docs);
})
.catch(function (err) {
response.status(500).send(err);
});
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