Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB驱动程序分页_Mongodb_Go_Echo_Mongo Go - Fatal编程技术网

MongoDB驱动程序分页

MongoDB驱动程序分页,mongodb,go,echo,mongo-go,Mongodb,Go,Echo,Mongo Go,目前,我可以退回我收藏的所有产品。 但是,我希望能够返回特定产品ID之后的产品(这将是客户端的最后一个产品ID,以便它们可以加载更多) 当前方式(全部返回) 新方式尝试(基于页面返回) 您可以构造一个查询,其中\u id大于afterID,在这种情况下,您还应该指定按\u id排序。对于排序和设置限制,您可以使用 您还应该使用来解码所有结果,而不是逐个解码 这就是它的样子: query := bson.M{"_id": bson.M{"$gt": afte

目前,我可以退回我收藏的所有产品。 但是,我希望能够返回特定产品ID之后的产品(这将是客户端的最后一个产品ID,以便它们可以加载更多)

当前方式(全部返回)

新方式尝试(基于页面返回)


您可以构造一个查询,其中
\u id
大于
afterID
,在这种情况下,您还应该指定按
\u id
排序。对于排序和设置限制,您可以使用

您还应该使用来解码所有结果,而不是逐个解码

这就是它的样子:

query := bson.M{"_id": bson.M{"$gt": afterID}}
opts := options.Find().
    SetSort(bson.M{"_id": 1}).
    SetLimit(10)
    
ctx := c.Request().Context()

curs, err := mg.Db.Collection("products").Find(ctx, query, opts)
if err != nil {
    // Handle error
}

var products []Returnedproduct
if err = curs.All(ctx, &products); err != nil {
    // Handle error
}

return c.JSON(http.StatusOK, products)

您可以构造一个查询,其中
\u id
大于
afterID
,在这种情况下,您还应该指定按
\u id
排序。对于排序和设置限制,您可以使用

您还应该使用来解码所有结果,而不是逐个解码

这就是它的样子:

query := bson.M{"_id": bson.M{"$gt": afterID}}
opts := options.Find().
    SetSort(bson.M{"_id": 1}).
    SetLimit(10)
    
ctx := c.Request().Context()

curs, err := mg.Db.Collection("products").Find(ctx, query, opts)
if err != nil {
    // Handle error
}

var products []Returnedproduct
if err = curs.All(ctx, &products); err != nil {
    // Handle error
}

return c.JSON(http.StatusOK, products)
该官员还有一个
*FindOptions
可选参数,您也可以查看

 pageOptions := options.Find()
 pageOptions.SetSkip(int64(page)) //0-i
 pageOptions.SetLimit(int64(limit)) // number of records to return

 cur, err := userCollection.Find(c.Request().Context(), bson.D{{}}, pageOptions)
 if err != nil {
    // handle error
 }

 defer cur.Close(ctx)
 var products []Returnedproduct
 for cur.Next(c.Request().Context()) {
    var product Returnedproduct
    if err := cur.Decode(&product); err != nil {
        // handle error
    }
    products = append(products, &product)
 }

 if err := cur.Err(); err != nil {
    // handle error
 }
该官员还有一个
*FindOptions
可选参数,您也可以查看

 pageOptions := options.Find()
 pageOptions.SetSkip(int64(page)) //0-i
 pageOptions.SetLimit(int64(limit)) // number of records to return

 cur, err := userCollection.Find(c.Request().Context(), bson.D{{}}, pageOptions)
 if err != nil {
    // handle error
 }

 defer cur.Close(ctx)
 var products []Returnedproduct
 for cur.Next(c.Request().Context()) {
    var product Returnedproduct
    if err := cur.Decode(&product); err != nil {
        // handle error
    }
    products = append(products, &product)
 }

 if err := cur.Err(); err != nil {
    // handle error
 }

我的ID是primitive.ObjectID,所以它们不能按数字排序?我假设@Henry ObjectID是有序的,它们可以排序,这大致相当于时间顺序(第二精度)。我的ID是primitive.ObjectID,所以它们不能按数字排序?我假设@Henry ObjectID是有序的,它们可以排序,大致相当于时间顺序(第二精度)。