If document in collection return it else insert it

Hi,
I want to achieve the following. I want to insert a document to a collection only if this document doesn’t exist inside the collection otherwise return the existing document.
Is it possible to do it with a single query?

Thank you,

Yes, you’ll need the following:

  • Use the findOneAndUpdate operation;
  • In the mutation/update parameter or the function, use the $setOnInsert operator to set the fields of the new document in case one is created.
  • In the options parameter, configure the upset and returnDocumet fields.