我正在学习scala和mongodb并使用该剧!框架,所以当我理解事物时,我犯了各种各样的错误。目前我有一个scala对象,它返回一个通过casbah从mongodb查询返回的数据库对象列表,如下所示;
object Alerts {
def list() : List[DBObject]= {
val collection = MongoDatabase.collection;
val query = MongoDBObject.empty
val order = MongoDBObject("Issue Time:" -> -1)
val list = collection.find(query).sort(order).toList
list
}
... }
在我的代码的其他地方,我希望在Json中输出对象列表 - 所以我有;
val currentAlerts = Alerts.list()
我想写的是这样的;
val resultingJson = currentAlerts.toJson
但是当我这样做时,我可以理解得到以下错误;
value toJson is not a member of List[com.mongodb.casbah.Imports.DBObject]
我的问题是 - 将com.mongodb.casbah.Imports.DBObject的List转换为Json输出的正确方法是什么?
编辑:
为清楚起见,我真正想做的就是相当于
val listInJson = collection.find(query).sort(order).toJson
就像我写的一样
val listAsString = collection.find(query).sort(order).toString