Created
February 28, 2019 12:50
-
-
Save sibelius/44192680edfcad62c4fab403a26d8c41 to your computer and use it in GitHub Desktop.
How to safe update nested fields on mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dot from 'dot-object'; | |
export const safeNestedPath = (args: object) => { | |
return dot.dot(args); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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