【问题标题】:How to "sort" and "limit" results in mongodb?如何在mongodb中“排序”和“限制”结果?
【发布时间】:2018-12-13 05:51:47
【问题描述】:

我正在尝试使用“排序”和“限制”执行查询。使用mgo 你可以做Find(nil).Sort(“-when”).Limit(10) 但new, official mongo driver 没有这样的方法。如何使用新驱动程序进行排序和“限制”?

【问题讨论】:

  • 大概你会使用findopt.Sort 来构建Find 的选项。不幸的是,它需要一个interface{} 参数,文档说明没有任何用处,文档或 GitHub 中也没有示例。

标签: mongodb go


【解决方案1】:

在当前版本mongo-go-driver v1.0.3 中,选项被简化。例如执行查找、排序和限制:

import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

options := options.Find()

// Sort by `_id` field descending
options.SetSort(bson.D{{"_id", -1}})

// Limit by 10 documents only 
options.SetLimit(10)

cursor, err := collection.Find(context.Background(), bson.D{}, options)

在godoc.org/go.mongodb.org/mongo-driver/mongo/options 上查看更多可用选项。尤其是FindOptions 对于Find() 的所有可能选项。

【讨论】:

    【解决方案2】:

    官方驱动并不像mgo那么简单。您可以使用findopt.Limit 和findopt.Sort 进行排序和限制。

    您可以从官方存储库中查看示例。

    https://github.com/mongodb/mongo-go-driver/blob/5fea1444e52844a15513c0d9490327b2bd89ed7c/mongo/crud_spec_test.go#L364

    【讨论】:

    • 不直截了当是轻描淡写。完全是垃圾
    • 这个已被弃用。请改用options.Find()。
    【解决方案3】:

    你可以使用

    findOptions := options.Find()
    findOptions.SetLimit(2)
    findOptions.SetSkip(2)
    ...
    cursor, err := collection.Find(context.Background(), bson.M{}, findOptions)
    

    资源https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial

    【讨论】:

      【解决方案4】:

      您需要导入“github.com/mongodb/mongo-go-driver/options”包来构建findOptions。

      import github.com/mongodb/mongo-go-driver/options
      
      findOptions := options.Find() // build a `findOptions`
      findOptions.SetSort(map[string]int{"when": -1}) // reverse order by `when`
      findOptions.SetSkip(0) // skip whatever you want, like `offset` clause in mysql
      findOptions.SetLimit(10) // like `limit` clause in mysql
      
      // apply findOptions
      cur, err := collection.Find(context.TODO(), bson.D{}, findOptions)
      // resolve err
      
      for cur.Next(context.TODO()) {
         // call cur.Decode()
      }
      
      

      【讨论】:

        【解决方案5】:

        排序选项显然要求您添加一个map[string]interface{},您可以在其中指定一个字段作为键和一个sortOrder 作为值(其中1 表示升序,-1 表示降序)如下:

        sortMap := make(map[string]interface{})
        sortMap["version"] = 1
        opt := findopt.Sort(sortMap)
        

        据我所知,这意味着您只能按一个 sortField 正确排序结果,因为 go map 中的键是以随机顺序存储的。

        【讨论】:

          【解决方案6】:

          单行选项

          我知道已经有很多答案了,但是你可以一行来做(如果你需要它来解决你的任何情况)

          // From the Doc
          // func (f *FindOptions) SetSort(sort interface{}) *FindOptions
          
          cursor, err := collection.Find(context.Background(), bson.M{}, options.Find().SetSort(map[string]int{"when": -1}).SetLimit(10))
          

          SetSort() 等 当前返回父指针本身

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-01-05
            • 1970-01-01
            • 2017-09-14
            • 1970-01-01
            • 1970-01-01
            • 2020-08-14
            • 2012-05-09
            相关资源
            最近更新 更多