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.
If you wish your application to have the complete document:
At that point the document you have is exactly the document that the database has, and returning it from the insert is pointless.
Some other databases generate ids on the server side, but in case of MongoDB each driver implements id generation on the client side such that each document can be completely known prior to the insert.