我应该保留 setLimit(int64) 方法来显示 Collections 中的所有记录
What should i keep setLimit(int64) method to display all the records in Collections
IN setLimit() 方法我应该保留什么来获取数据中的所有记录
包 - 使用:go.mongodb.org/mongo-driver/bson
go.mongodb.org/mongo-driver/mongo
go.mongodb.org/mongo-driver/mongo/选项
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)
如果您不想限制结果数量,最简单的方法是不调用 FindOptions.SetLimit()
。如果您不传递 FindOptions
,或者传递未设置限制的条件,则默认情况下,结果不受限制。
如果您有一个 FindOptions
值,其中之前设置了限制,您可以将限制设置为 0
到 "undo" 限制。
// 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
IN setLimit() 方法我应该保留什么来获取数据中的所有记录
包 - 使用:go.mongodb.org/mongo-driver/bson
go.mongodb.org/mongo-driver/mongo
go.mongodb.org/mongo-driver/mongo/选项
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)
如果您不想限制结果数量,最简单的方法是不调用 FindOptions.SetLimit()
。如果您不传递 FindOptions
,或者传递未设置限制的条件,则默认情况下,结果不受限制。
如果您有一个 FindOptions
值,其中之前设置了限制,您可以将限制设置为 0
到 "undo" 限制。
// 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