mongoose-materialized icon indicating copy to clipboard operation
mongoose-materialized copied to clipboard

save dynamically deep document

Open DJWassink opened this issue 9 years ago • 4 comments

I wan't to dynamically update a document (so I don't know which key/values get passed to the save function). And the key/values can be nested prety deep, Once again you don't know how deep.

Currently I do this by first getting the document from the database and extend it with the data passed to the update. However this way the document also get converted to a plain object (toObject()).

Big downside of this is that we can't use the .save() method any more because it has become a plain object. Big no no of this story is that the parentId is (maybe) updated in the progress but the path doesn't get updated.

Is there some way to dynamically update a deep nested document while also updating the materialized path?

Maybe it's a idea to also make a method which will only update a specific document its path (or starting from that document it's path and recursively do all it's children), much like the .Building does now but only from the updated document it's id.

Note: to explain it a bit more, this is what is basically wanna do but in less functions (somehow this doesn't work though. The parentId does get updated but the path doesn't)

node = extend(true, node.toObject(), data);

Node.findByIdAndUpdate(data._id, node, function(err, doc) {
    Node.findById(doc._id, function(err, savedNode) {
        console.log(savedNode.parentId);
        savedNode.setParent(savedNode.parentId);
        savedNode.save(callback);
    });
});

DJWassink avatar Aug 20 '14 13:08 DJWassink