Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  18] [ 2]  / answers: 1 / hits: 8892  / 10 Years ago, thu, june 12, 2014, 12:00:00

UPDATE 1: 5 votes have been received, so I have submitted a feature request: https://github.com/LearnBoost/mongoose/issues/2637



Please cast your +1 votes there to let the core team know you want this feature.






UPDATE 2: See answer below...






ORIGINAL POST:



Lets say I do a lean query on a collection OR receive some data from a REST service and I get an array of objects (not mongoose documents).



These objects already exist in the database, but I need to convert some/all of those objects to mongoose documents for individual editing/saving.



I have read through the source and there is a lot going on once mongoose has data from the database (populating, casting, initializing, etc), but there doesn't seem to be a method for 'exposing' this to the outside world.



I am using the following, but it just seems hacky ($data is a plain object):



// What other properties am I not setting?  Is this enough?
var doc = new MyModel( $data );
doc.isNew = false;

// mimicking mongoose internals
// init is called internally after a document is loaded from the database
// This method is not documented, but seems like the most proper way to do this.
var doc = new MyModel( undefined );
doc.init( $data );


UPDATE: After more searching I don't think there is a way to do this yet, and the first method above is your best bet (mongoose v3.8.8). If anybody else is interested in this, I will make a feature request for something like this (leave a comment or upvote please):



var doc = MyModel.hydrate( $data );

More From » mongodb

 Answers
13

Posting my own answer so this doesn't stay open:



Version 4 models (stable released on 2015-03-25) now exposes a hydrate() method. None of the fields will be marked as dirty initially, meaning a call to save() will do nothing until a field is mutated.



https://github.com/LearnBoost/mongoose/blob/41ea6010c4a84716aec7a5798c7c35ef21aa294f/lib/model.js#L1639-1657



It is very important to note that this is intended to be used to convert a plain JS object loaded from the database into a mongoose document. If you are receiving a document from a REST service or something like that, you should use findById() and update().



For those who live dangerously:



If you really want to update an existing document without touching the database, I suppose you could call hydrate(), mark fields as dirty, and then call save(). This is not too different than the method of setting doc.isNew = false; as I suggested in my original question. However, Valeri (from the mongoose team) suggested not doing this. It could cause validation errors and other edge case issues and generally isn't good practice. findById is really fast and will not be your bottleneck.


[#44612] Wednesday, June 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samarab

Total Points: 620
Total Questions: 95
Total Answers: 89

Location: Bonaire
Member since Wed, May 11, 2022
2 Years ago
;