Collection.InsertOne()
returns a *InsertOneResult
, which only contains the ID of the inserted document. To get the inserted document, you have to perform another Collection.Find()
query. Is there a way to do this in a single step?
A current work around is to use Collection.FindOneAndUpdate()
with Upsert
set to true, as this returns a *SingleResult
that can then be decoded into a struct, and sent back to the client.
InsertOne()
expects the document to be inserted, so you must have it when calling it. The only "optional" thing is the_id
which is returned to you in theInsertOneResult
. Please explain what you want to achieve. – icza