Skip to content

Instantly share code, notes, and snippets.

@asvid
Created October 4, 2016 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asvid/0ebdfa67b3f630a0c99c05f63b6120da to your computer and use it in GitHub Desktop.
Save asvid/0ebdfa67b3f630a0c99c05f63b6120da to your computer and use it in GitHub Desktop.
MongoDB save or update
var query = {ean: productJson.ean},
options = {upsert: true, new: true, setDefaultsOnInsert: true};
Product.findOneAndUpdate(query, productJson, options, function (error, item) {
if (!!error) {
res.status(400).json(error)
} else {
res.status(201).json({"msg": "document created"})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment