1

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.

4

1 回答 1

1

如果您希望您的申请有完整的文件:

  • 在客户端生成_id
  • 插入完整的文档

那时,您拥有的文档正是数据库拥有的文档,从插入中返回它是没有意义的。

其他一些数据库在服务器端生成 id,但在 MongoDB 的情况下,每个驱动程序都在客户端实现 id 生成,以便在插入之前可以完全知道每个文档。

于 2020-08-27T20:08:29.963 回答