Scala 如何使用reactivemongo覆盖Play框架内的gridfs文件?

Scala 如何使用reactivemongo覆盖Play框架内的gridfs文件?,scala,playframework,playframework-2.0,reactivemongo,mongodb-scala,Scala,Playframework,Playframework 2.0,Reactivemongo,Mongodb Scala,我有以下代码来编写gridfs文件: request.body.files.toList.lastOption match { case Some(picture) => { val filename = picture.filename val contentType = picture.contentType picture.ref.moveTo(new File("/tmp/" + filename), true) val

我有以下代码来编写gridfs文件:

  request.body.files.toList.lastOption match {
    case Some(picture) => {
      val filename = picture.filename
      val contentType = picture.contentType
      picture.ref.moveTo(new File("/tmp/" + filename), true)

      val gridFS = new GridFS(db, "attachments")
      val fileToSave = DefaultFileToSave(filename, contentType)

      val futureResult: Future[ReadFile[BSONValue]] = gridFS.writeFromInputStream(fileToSave, new FileInputStream(new File("/tmp/" + filename)))
      ...
代码运行良好,但当我编写两个同名文件时,它会复制集合中的文件。我想用filename字段创建一个唯一的索引,但这样会保留第一个文件,我需要保留最新版本。我怎么做

谢谢

GA

  • 删除文件名的索引
  • 上载新文件,如果多个文件版本具有相同的名称就可以了
  • 将查询重写为以下内容:find({filename:“some file name.txt”}).sort({uploadDate:-1}).limit(1),因此只能得到最新的
  • 您可以使用新上载文件的时间戳删除具有相同名称和较小时间戳的所有文件