1

在 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)
4

1 回答 1

3

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
于 2020-02-24T12:53:29.400 回答