return Model.create(arr).exec() is not working in mongoose

Related searches

I heard that exec "returns a promise" so I'm using exec it do asynchronous calls. This question is inspired my other question. The commentator said my code is not working because :

You are using asynchronous code synchronously

I was trying to fix that by using the below code. Don't know if this code will make it not sync but I heard that promises help with that.

so I have this and I cannot create(save) the data but I can delete it. why cant I use the same pattern for create as i did for remove?

var Comp = require("./models/company.js");
  var arr = [
    {name : "comp1",industry : "industry1", ranking: 20},
    {name : "comp2",industry : "industry2", ranking: 5},
    {name : "comp3",industry : "industry3", ranking: 10}
  ]


Comp.find({}).exec()
    .then(function(docs){
        return Comp.remove({}).exec()
        .then(function(){
            console.log("deleted")
        })
    })
    .then(function(){
        return Comp.create(arr).exec()
        .then(function(data){
            console.log(data)
        })
    })

and can you help get to my original goal which was in my other question.

The then funtion does not return promise, the exec does!

So you need to do return Comp.remove({}).exec()

Comp.find({}).exec()
.then(function(docs){
    return Comp.remove({}).exec();
})
.then(function(result_of_remove){
    return Comp.create(arr).exec();
})
.then(function(result_of_create){
    ....
})

Support / feature request Model.create().exec() � Issue #5060 , The problem is that (Model).create().exec() is not a valid method. else { reject( error); } return; } if (doc instanceof Array) { resolve(savedDocs);� Sends multiple insertOne, updateOne, updateMany, replaceOne, deleteOne, and/or deleteMany operations to the MongoDB server in one command. This is faster than sending multiple independent operations (e.g. if you use create()) because with bulkWrite() there is only one round trip to MongoDB.

first of all you should confirm you mongoose version.

in older version:

Model.create(doc) returns a query object; call the exec method of the query will trigger the database operation and return a promise.

in new version (i am using 4.4.8) of mongoose Model.create(doc) and 'Model.remove(con)' returns a promise directly.

so check with your version to see if you need to remove some exec

last but not least add catch call to check if you got some errors, it helps when debug

Comp.find({}).exec()
.then(function(docs){
    return Comp.remove({}).exec();
})
.then(function(result_of_remove){
    return Comp.create(arr).exec();
})
.then(function(result_of_create){
    ....
})
.catch(function(error){
  console.log(error)
})

Mongoose v5.9.28:, Returns: �Object�. Casts this query to the schema of model on this query. If this flag is not null or undefined, the exec() promise will reject without executing. But i saw a difference in the mongoose/lib/model.js : if i console.log(conditions) at the end of Model.find function i 've got { packId: '55083c370e5fcc8e1ea7984c' } in the case of static value => return results { packId: 55083c370e5fcc8e1ea7984c } in the case of objectId => empty array ( note the quotes )

I normally use .exec() when I want to return a Promise when working with

Model.findOne(...).exec()

Model.create(...) returns a Promise.

The .exec() function does not exist for Model.create(...)

Mongoose v5.9.28: API docs, Do not use this to create a new Decimal128 instance, use mongoose. A socket may be inactive because of either no activity or a long-running operation. Calling createConnection() adds a connection to this array. Returns: � Mongoose� this. Removes the model named name from the default connection, if it exists. Making it return null would be a huge backwards breaking change and probably not ideal since you would want to know if the value being passed in is not being cast properly. I'll let @vkarpov15 chime in though on whether or not that's a feature worth exploring.

How find() Works in Mongoose, Make no mistake, Model.find() does what you expect: find all you already have a MongoDB instance running on localhost:27017 . The Model.find() function returns an instance of Mongoose's Query class. const q = Customer.find(); q instanceof Promise; // false q.exec() instanceof Promise; // true� Getter/setter around the current mongoose-specific options for this query Below are the current Mongoose-specific options. populate: an array representing what paths will be populated. Should have one entry for each call to Query.prototype.populate() lean: if truthy, Mongoose will not hydrate any

Express Tutorial Part 3: Using a Database (with Mongoose), While there is no single "best" database, almost any of the popular Mongoose: Mongoose is a MongoDB object modeling tool designed to work in an Models are created from schemas using the mongoose.model() method: a later time query.exec(function (err, athletes) { if (err) return handleError(err);� We've covered the basics of making queries in the MongoDB Shell, but since we'll mostly be writing our queries in our server's JavaScript code using Mongoose, it makes sense to understand how Mongoose does queries. We'll be covering basic CRUD (Create, Read, Update, Delete) operations. With Mongoose, you can perform

The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned). Mongoose does not cast pipeline stages. The below will not work unless _id is a string in the database

Comments
  • Important information. .create returns a promise so you shouldn't use .exec() stackoverflow.com/questions/37358133/… .. That's why I got the error.
  • Thanks for the clarification about the versions
  • Yup, that was my issue: the .exec() on my create() method was not working. Removed the .exec() and now it returned properly. thank you.