如果可能的话,我想在 insertOne 操作后返回 mongo 生成的 ObjectId()
def insert(inputDocument : Map[String,Any], databaseName : String, collectionName : String, mongoClient : MongoClient)
: Future[Completed] = {
implicit val formats: DefaultFormats.type = DefaultFormats
val createdAt = new Date()
val createdAtUnix = System.currentTimeMillis()
val insertJson = write( inputDocument ++ Map("createdAt" -> createdAt, "createdAtUnix" -> createdAtUnix))
val database: MongoDatabase = mongoClient.getDatabase(databaseName)
val collection: MongoCollection[Document] = database.getCollection(collectionName)
val insertDocument = Document(insertJson)
collection
.insertOne(insertDocument)
.toFuture()
}
如果不可能,我知道可以生成一个,但是如何确保它是唯一的?