Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose create multiple documents

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?

like image 924
lostintranslation Avatar asked Mar 14 '13 02:03

lostintranslation


People also ask

What is difference between create and save in mongoose?

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.

Does Mongoose model create collection?

Mongoose never create any collection until you will save/create any document.

What is Mongoose create?

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]'

How do I create a new document in mongoose?

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:

How do I insert multiple documents in MongoDB?

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)

How do I connect a model to a mongoose database?

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.

What is the difference between create () and insertmany () in mongoose V5?

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.


2 Answers

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
    }
});
like image 162
JohnnyHK Avatar answered Oct 07 '22 21:10

JohnnyHK


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);
    });
like image 43
Nebojsa Sapic Avatar answered Oct 07 '22 19:10

Nebojsa Sapic