scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
556 stars 80 forks source link

nedb findOneAndUpdate with Embedded Document #81

Closed jssuttles closed 8 years ago

jssuttles commented 8 years ago

I just ran into a problem in which I was trying to findOneAndUpdate (with upsert: true) a new Document with an Embedded Document. The problem is that NeDB's deepcopy function doesn't save the Embedded Document's _schema correctly. Since the types in _schema are functions (i.e. String is a function), these get returned as undefined, which in Base Documents _fromData function it tries to check the documentClass of the undefined type. Possibly the _fromData function needs to know which _schema are available and rebuild it on the fly.

jssuttles commented 8 years ago

Either that or just append the _id, like you do for save().

michaeljota commented 8 years ago

If you need an _id for an Embedded Document, then you don't need a Embedded Document. Just extends a regular Document from which you could use findOneAndUpdate.

Now, the problem as I see here, it's findOneAndUpdate in a id-less Embedded Document. Those functions may confuse you, and I think should be removed. :)

jssuttles commented 8 years ago

I think I might have misunderstood findOneAndUpdate. The value needs to be a normal object. It can't be a Document or contain EmbeddedDocuments. Is that right?

jssuttles commented 8 years ago

Alright. I figured it out. It does work. Just don't include an EmbeddedDocument in the value.