Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created February 28, 2019 12:50
Show Gist options
  • Save sibelius/44192680edfcad62c4fab403a26d8c41 to your computer and use it in GitHub Desktop.
Save sibelius/44192680edfcad62c4fab403a26d8c41 to your computer and use it in GitHub Desktop.
How to safe update nested fields on mongoose
import dot from 'dot-object';
export const safeNestedPath = (args: object) => {
return dot.dot(args);
};
const updatedPayload = safeNestPath({
nested: {
downThere: {
name: 'awesome',
}
}
})
// same as: $set: 'nested.downThere.name': 'awesome'
await MyModel.updateOne({_id }, { $set: updatePayload })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment