Created
April 20, 2020 18:38
-
-
Save mattwang44/034ef80404a47d6ae87b249e7483b7a7 to your computer and use it in GitHub Desktop.
Mongo query tips
This file contains hidden or 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
// findOneOrCreate in Mongoose: | |
// Return the original doc (no modification) if it can be found and create a new doc if cannot be found. | |
// Without `$setOnInsert`, it will update the doc if it can be found. | |
Model.findOneAndUpdate( | |
{name: 'Matt'}, | |
{$setOnInsert: {score: 60}}, | |
{upsert: true, new: true} | |
) | |
// Set `setDefaultsOnInsert` to true in option if need to insert with setting the default value. | |
// See https://mongoosejs.com/docs/defaults.html#the-setdefaultsoninsert-option |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment