how use offset and limit with mongodb?

huangapple go评论87阅读模式
英文:

how use offset and limit with mongodb?

问题

你好!以下是你要翻译的内容:

如何在Go语言中使用offset和limit与mongodb一起使用?

我需要一个offset来确定我首先获取的API的顺序,我还需要一个limit来指定要显示的API数量。有人曾经做过这样的事吗?

图片中的描述how use offset and limit with mongodb?

英文:

How can I use offset and limit in Go with mongodb?

I need an offset to determine order of APIs I fetch first, and I need a limit to specify number of APIs to display. Has anyone ever made one?

Description in picturehow use offset and limit with mongodb?

答案1

得分: 2

你可以使用go.mongodb.org/mongo-driver/mongo包来实现这个功能。

SetSkip方法用于指定在返回结果之前要跳过的文档数量(即偏移量)。

SetLimit方法用于限制结果的数量。

options是从go.mongodb.org/mongo-driver/mongo/options导入的。

db.Collection("users").Find(ctx, bson.M{}, options.Find().SetSkip(offset).SetLimit(limit))
英文:

You can use go.mongodb.org/mongo-driver/mongopackage for that.

SetSkip specifies the number of documents to skip before returning (it is offset).

SetLimit specifies a limit on the number of results.

options are imported from go.mongodb.org/mongo-driver/mongo/options

db.Collection("users").Find(ctx, bson.M{}, options.Find().SetSkip(offset).SetLimit(limit))

答案2

得分: 0

你可以按照以下方式跳过和限制每个集合中的文档:

db.users.find({}).skip(1).limit(10).sort({_id:1})

你还可以使用mongoose-paginate包来实现这个功能。它还可以提供排序、填充、跳过和限制文档等功能。

var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');

var userSchema = new mongoose.Schema({
    name: {
        type: String
    }
});
userSchema.plugin(mongoosePaginate);

Mongoose paginate接受两个参数作为输入,分别是filter和options。

db.users.paginate(filter, options)

filter是搜索条件,而options具有以下字段:

  • select:选择结果中的哪些字段
  • sort:按照哪个字段排序以及排序顺序
  • populate:如果有ID的引用,则可以进行填充
  • lean:是否将结果转换为JavaScript对象
  • leanWithId:是否在JavaScript对象中将ObjectId转换为字符串
  • offset:要跳过的文档数量
  • limit:每页显示的文档数量
英文:

You can skip and limit the documents per collection as follow

db.users.find({}).skip(1).limit(10).sort({_id:1})

You can also use the mongoose-paginate package for that.
It can also provide the feature like sort and populate, along with skip and limit of documents.

var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');

var userSchema = new mongoose.Schema(
                {     name:{
                           type:String
                           }
                });
userSchema.plugin(mongoosePaginate);

Mongoose paginate takes two arguments as input ,which are filter and options

db.users.paginate(filter,options)

filter is the search criteria while options has the following fields:

> select: Which fields to select from the result
>
> sort: On which field to sort and in which order
>
> populate :If there is reference of Id then the populate can be done
> externally.
>
> lean: Should convert the result to js Object or not.
>
> leanWithId: Should also convert ObjectId to string in js Object.
>
> offset: How many documents to skip.
>
> limit: The number of documents per page to be displayed.

huangapple
  • 本文由 发表于 2021年12月16日 16:47:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/70376155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定