【问题标题】:What should i keep setLimit(int64) method to display all the records in Collections我应该保留什么 setLimit(int64) 方法来显示集合中的所有记录
【发布时间】:2020-02-24 20:56:08
【问题描述】:

IN setLimit() 方法我应该保留什么来获取数据中的所有记录

包 - 使用:go.mongodb.org/mongo-driver/bson

go.mongodb.org/mongo-driver/mongo

go.mongodb.org/mongo-driver/mongo/options

findOption := options.Find()

findOption.SetLimit(?)

var res1 []Person

cur, err := collection.Find(context.TODO(), bson.D{}, findOption)

if err != nil {
    log.Fatal(err)
}

for cur.Next(context.TODO()) {

    var elem Person

    err := cur.Decode(&elem)

    if err != nil {
        log.Fatal(err)
    }

    res1 = append(res1, elem)
}

if err := cur.Err(); err != nil {
    log.Fatal(err)
}

// Close the cursor once finished
cur.Close(context.TODO())

fmt.Printf("Found multiple documents (array of pointers): %+v\n", res1)

【问题讨论】:

    标签: mongodb go mongo-go


    【解决方案1】:

    如果您不想限制结果的数量,最简单的方法是不要调用FindOptions.SetLimit()。如果你没有通过FindOptions,或者你通过了一个你没有设置限制的地方,默认情况下,结果是不受限制的。

    如果您有一个 FindOptions 值,之前已设置了限制,您可以设置一个 0 的限制来“撤消”限制。

    引用FindOptions.Limit:

    // The maximum number of documents to return. The default value is 0, which means that all documents matching the
    // filter will be returned. A negative limit specifies that the resulting documents should be returned in a single
    // batch. The default value is 0.
    Limit *int64
    

    【讨论】:

      猜你喜欢
      • 2012-11-18
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多