package repos
import javax.inject.Inject
import play.api.libs.json.{JsObject, Json}
import play.modules.reactivemongo.{ReactiveMongoApi, ReactiveMongoComponents,ReactiveMongoPlugin}
import play.modules.reactivemongo.json._
import play.modules.reactivemongo.json.collection.JSONCollection
import reactivemongo.api.{QueryOpts, ReadPreference}
import reactivemongo.api.commands.WriteResult
import reactivemongo.bson.{BSONDocument, BSONObjectID}
import reactivemongo.bson._
import reactivemongo.api.collections.bson.BSONCollection
import scala.language.existentials
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Try, Success, Failure}
import models._
import play.api.Logger
import play.api.libs.iteratee.Enumerator
trait MongoDao[T] extends DaoHelper{
val collectionName: String
import play.api.Play.current
lazy val reactiveMongoApi = current.injector.instanceOf[ReactiveMongoApi]
def collection = reactiveMongoApi.db.collection[BSONCollection](collectionName)
/**
* Inserts a list of T into mongo
*
* @param docList - List of T
* @param writer - The BSONDocumentWriter on the companion object for T
* @return - a Future[Try[Int]]
*/
def insert(docList: List[T])(implicit writer: BSONDocumentWriter[T]): Future[Try[Int]] = {
Logger.debug(s"Inserting document: [collection=$collectionName, data=$docList]")
val enumerator = Enumerator.enumerate(docList.map( t => writer.write(t)))
collection.bulkInsert(enumerator).map{ Success(_)}
}
}
任何人都知道问题来自哪里?