When using mongodb library in golang, I have a field that is unique “_id”.
I want to be able to get the object in case I face a duplicate..
Is it possible in one command only instead of having to do something like:
(try to insert, if duplicate, get job)
<code>_, err := s.coll.InsertOne(ctx, entry)
if err != nil {
if mongo.IsDuplicateKeyError(err) {
duplicateJob, loadErr := f.jobStore.loadPendingJob(ctx, *job.JobID)
}
}
</code>
<code>_, err := s.coll.InsertOne(ctx, entry)
if err != nil {
if mongo.IsDuplicateKeyError(err) {
duplicateJob, loadErr := f.jobStore.loadPendingJob(ctx, *job.JobID)
}
}
</code>
_, err := s.coll.InsertOne(ctx, entry)
if err != nil {
if mongo.IsDuplicateKeyError(err) {
duplicateJob, loadErr := f.jobStore.loadPendingJob(ctx, *job.JobID)
}
}
Thanks in advance