I want to create a new document if it doesn't exist and return the new document once created, however if the document already exists I would like an exception to be thrown.
I think the way I am doing this seems hackish, is there a better means of doing this?
var query = { name: 'guaranteed to be unique' }
var new_doc = { ... }
var col = // the Mongo collection we're using
col.updateOne(
query,
new_doc,
{upsert: true}
)
.then(update => col.findOne({_id, update.result.upserted.pop()._id}))
.then(doc => console.log(doc))
.catch( exception => console.log('that already exists') )