【问题标题】:Sort by multiple attributes with Salat for MongoDB使用 Salat for MongoDB 按多个属性排序
【发布时间】:2013-07-05 18:01:26
【问题描述】:

我尝试使用 Salat 在 MongoDB 上执行查询。查询应按两个属性对结果集进行排序。我没有找到任何例子。使用单个 MongoDBObject 进行排序按预期工作。

val results = dao
  .find(MongoDBObject.empty)
  .sort(orderBy = MongoDBObject("attribute1" -> 1))
  .skip(0)
  .limit(10).toList

如何组合两个 MongoDBObject 以便由 orderBy 评估?例如,如果我的优先级排序是按属性 1 升序排序,而我的次要排序是按属性 2 升序排序。

感谢您的帮助!

【问题讨论】:

    标签: mongodb scala salat


    【解决方案1】:

    您可以按如下方式构建排序对象:

    val sort = MongoDBObject("attribute1" -> 1) ++ ("attribute2" -> -1)
    

    然后进行查询:

    val results = dao
      .find(MongoDBObject.empty)
      .sort(orderBy = sort)
      .skip(0)
      .limit(10).toList
    

    【讨论】:

      【解决方案2】:

      您可以进行以下更改:

      val results = dao
        .find(MongoDBObject.empty)
        .sort(orderBy = MongoDBObject("attribute1" -> 1, "attribute2" -> -1))
        .skip(0)
        .limit(10).toList
      

      现在您将按升序对属性 1 进行优先排序,在平局上按降序排序到属性 2。这使您可以在一行中完成所有操作,而不是创建列表连接答案中需要的中间值。

      【讨论】:

        猜你喜欢
        • 2015-02-06
        • 1970-01-01
        • 2013-01-02
        • 1970-01-01
        • 1970-01-01
        • 2012-02-22
        • 1970-01-01
        • 2011-11-24
        • 2016-01-06
        相关资源
        最近更新 更多