Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using lean for save? #1723

Closed
imrefazekas opened this issue Oct 3, 2013 · 21 comments
Closed

Using lean for save? #1723

imrefazekas opened this issue Oct 3, 2013 · 21 comments

Comments

@imrefazekas
Copy link

I would like to use lean option as for save.
The callback of save returns also a mongoose document and not a JS object which I want to change using a lean-like call.

Using lean for queries and toObject in saves' callback functions seems like a confusion to me.

Any ideas?

@nadeesha
Copy link

nadeesha commented Oct 9, 2013

If you don't want to use lean, then your best bet is to form a new object from the returned Mongoose object

Person.findOne({
    _id: req.user.id
}, function(err, result) {
    if (err)
        return next(err);

    return (200, {
        id: result._id,
        spouse: result.spouse,
        children: result.children
    });
});

@imrefazekas
Copy link
Author

Maybe my post was not clear, sorry.
I would like to use the lean but on saving operations as well.
The callbacks of the save functions like "save" or "update" return with schema object and I cannot use lean to have a pure JS object.
This is my problem. I would like to use lean for save and update as well.

@refack
Copy link
Contributor

refack commented Oct 13, 2013

@imrefazekas, Save, Update, Create, and Remove are not "queries", so they don't take the .lean() directive as is.
You could use the .toObject() method on the returned object to get it without the schema:

p.save(function (err, returned) {
  var leanObject = returned.toObject(); // has many options: http://mongoosejs.com/docs/api.html#document_Document-toObject
  assert.equal(leanObject.schema, null);
});

@ORESoftware
Copy link

ORESoftware commented Dec 22, 2015

hummity hums...save() should have a lean option?

@vkarpov15
Copy link
Collaborator

How would you imagine save() and lean working together? AFAIK that doesn't make sense.

@ORESoftware
Copy link

ummm, it's a been a few days but currently it's like so

user.save(function(err,user){

      //user is full-fledged etc
});

with lean option, it would/could be

user.save({
  lean:true
},function(err,user){

      //user is *not* full-fledged etc
});

isn't that right?

@vkarpov15
Copy link
Collaborator

@ORESoftware nope, because the 2nd parameter in the callback to save is the exact same document. Mongoose doesn't ask the database for the newly updated doc. For instance,

user.save(function(err, _user){
  user === _user; // true
});

@ORESoftware
Copy link

ORESoftware commented Jan 4, 2016

oh ok got it :) my bad

@peterpeterparker
Copy link

for the record, the solution provided above by @refack still work like a charm in 2017

@alex-dixon
Copy link

@peterpeterparker I disagree and think this is still an issue for users who want data to be returned from all database operations instead of Mongoose instances.

@peterpeterparker
Copy link

@alex-dixon I was just pointing out that the solution of @refack for the save operation is still valid and functional in 2017, nothing more, nothing less ;)

@alex-dixon
Copy link

@peterpeterparker You're right. Sorry. Shouldn't have directed that toward you.

@peterpeterparker
Copy link

@alex-dixon no worries

@ORESoftware
Copy link

should we use lean() with Model.create()?

@vkarpov15
Copy link
Collaborator

Never because you can't use lean() with Model.create() :p

@ORESoftware
Copy link

got it :)

@billouboq
Copy link
Contributor

billouboq commented Oct 22, 2021

@vkarpov15 Hi, out of curiosity why can"t we add lean on Model.create ?
If it's not possible, can we create a new method that will create a document, return the new created document and be leaned ?

Edit :
I can just use findOneAndUpdate with upsert true for this, but does it fail if schema is not correct ?

@vkarpov15
Copy link
Collaborator

@billouboq because using lean with Model.create() doesn't make much sense: create() instantiates a new document and inserts it into MongoDB. Whereas lean just skips creating a document and gives you back a POJO form of existing documents in the database.

If you want to use create() to create a new doc with schema validation, but then get a POJO back, you can just do await Model.create(doc).then(doc => doc.toObject())

@billouboq
Copy link
Contributor

For perf reasons it can happen a lot that we need to create a doc and have created document back, all of this with mongoose schéma validation but still returning a simple doc instead of a mongoose one, so toObject is not an option for us :/

@vkarpov15
Copy link
Collaborator

You can try Model.validate() followed by insertOne(): https://mongoosejs.com/docs/api/model.html#model_Model.validate . That will validate a raw object against your schema and then insert it into MongoDB. Try that out and see if the perf benefit is worth it. lean() is primarily useful because find() can return a lot of documents, so lean() can reduce the memory overhead. Unless you have very complex documents or a lot of documents, I doubt the perf improvement of avoiding create() would be significant relative to a network round trip.

@billouboq
Copy link
Contributor

Nice explanation, thanks for the tips ! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants