如何为group by编写MongoDBObject和case类

时间:2016-02-20 15:03:00

标签: mongodb scala salat

我是mongo,scala和salat的新手

我有json集合

{[
    {"firstName":"John", "lastName":"Doe", department="IT", skills="java"},
    {"firstName":"Anna", "lastName":"Smith", department="accounts, skills="tally"},
    {"firstName":"Peter", "lastName":"Jones", department="It" skills="java" }
]}

我将案例类定义如下

case class Employee(firstName:String,lastName:String,department:String,skills:String)

使用salatdao如下

object EmployeeDao extends SalatDAO[Employees, ObjectId](collection =employee_collection)

和让员工这样的方法

  def getEmployees(IT : String):List[Employees]={

    val listOfEmployees:List[Employees] = OrderDAO3.find(MongoDBObject({"department"->IT})).toList

    return listOfEmployees
  }

得到结果......

{[     {" firstName":" John"," lastName":" Doe",department =" IT",skills = "的java"},     {" firstName":" Peter"," lastName":" Jones",department =" IT"技能="的java" }   ]}

到目前为止一切顺利......现在我想要GROUP BY ALL DEPARTMENTS并想要这样的结果

  {"IT":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
  ]
  {"accounts":[
    {"firstName":"Johnac", "lastName":"Doeac"},
    {"firstName":"Annaac", "lastName":"Smithac"},
    {"firstName":"Peterac", "lastName":"Jonesac"}
 ]}
 }

所以请告诉我如何编写MongoDBObject来获得此结果,并建议我使用相应的案例类来保存该集合。