需要在mgo中使用分页

Raj*_*mar 1 pagination go mongodb mgo

我使用Go作为后端,使用MongoDB作为数据库,并使用beego框架来开发此应用程序。我想在前端进行分页。

我的结构看起来像这样:

type Employee struct {
        Name               string
        EmpId              string
        Password           string
        PhoneNumber        int32
        EmailAddress       string
        Position           string
        AccessLevel        string
        Gender             string
        MaritalStatus      string
        Nationality        string
        Department         string
        ICNumber           string
        JoinDate           time.Time
        ConfirmationDate   time.Time
        EndDate            time.Time
        AnnualLeave        []*AnnualLeaveInfo
        MedicalLeave       []*MedicalLeaveInfo
        NopayLeave         []*NopayLeaveInfo
        ChildcareLeave     []*ChildcareLeaveInfo
    }

type AnnualLeaveInfo struct {
        Id           int
        Days         float64
        Type         string
        From         time.Time
        To           time.Time
        AppliedDate  time.Time
        Status       string
        Certificate  []*CertificateInfo
    }
Run Code Online (Sandbox Code Playgroud)

其他请假信息与年假信息相同。我想显示所有雇员文档的所有叶子,这些叶子按适用日期和状态排序。现在,我要获取所有休假详细信息,然后追加到数组中,然后使用游标值(在上一个响应中传递到前端的数组的最后检索索引值)并从数组中查找值。我知道这不是一个好函数,因为对于每个请求,它都需要检索所有请假详细信息,以便仅检索前端请求的20个请假详细信息。

追加到数组后的结果是这样的

  "0": {
    "LeaveEmpId": "rajeshk",
    "Name": "rahul",
    "LeaveId": 8,
    "LeaveType": "annualleave",
    "LeaveTotal": 2,
    "LeaveDays": 1,
    "LeaveFrom": "2016-12-12T08:00:00+08:00",
    "LeaveTo": "2016-12-12T08:00:00+08:00",
    "LeaveAppliedDate": "2016-11-21T10:29:33.713+08:00",
    "LeaveStatus": "Processing",
    "CertificateName": [
      "1.pdf",
      "2.pdf"
    ]
  },
  "1": {
    "LeaveEmpId": "rajeshk",
    "LeaveName": "Rajesh",
    "LeaveId": 7,
    "LeaveType": "medicalleave",
    "LeaveTotal": 3,
    "LeaveDays": 1,
    "LeaveFrom": "2016-12-12T08:00:00+08:00",
    "LeaveTo": "2016-12-12T08:00:00+08:00",
    "LeaveAppliedDate": "2016-11-21T10:27:06.12+08:00",
    "LeaveStatus": "Processing",
    "CertificateName": [
      "1.pdf",
      "2.pdf"
    ]
  },
  "2": {
    "LeaveEmpId": "kumar",
    "LeaveName": "madia",
    "LeaveId": 6,
    "LeaveType": "nopayleave",
    "LeaveTotal": 4,
    "LeaveDays": 1,
    "LeaveFrom": "2016-12-12T08:00:00+08:00",
    "LeaveTo": "2016-12-12T08:00:00+08:00",
    "LeaveAppliedDate": "2016-11-21T10:11:58.283+08:00",
    "LeaveStatus": "Processing",
    "CertificateName": [
      "1.pdf",
      "2.pdf"
    ]
  },
  "3": {
    "LeaveEmpId": "master",
    "LeaveName": "kumar",
    "LeaveId": 5,
    "LeaveType": "nopayleave",
    "LeaveTotal": 5,
    "LeaveDays": 1,
    "LeaveFrom": "2016-12-12T08:00:00+08:00",
    "LeaveTo": "2016-12-12T08:00:00+08:00",
    "LeaveAppliedDate": "2016-11-21T10:02:17.565+08:00",
    "LeaveStatus": "Processing",
    "CertificateName": [
      "1.pdf",
      "2.pdf"
    ]
  },
  "4": {
    "LeaveEmpId": "rajeshk",
    "LeaveName": "Rajesh",
    "LeaveId": 4,
    "LeaveType": "medicalleave",
    "LeaveTotal": 6,
    "LeaveDays": 1,
    "LeaveFrom": "2016-12-12T08:00:00+08:00",
    "LeaveTo": "2016-12-12T08:00:00+08:00",
    "LeaveAppliedDate": "2016-11-21T09:52:20.185+08:00",
    "LeaveStatus": "Processing",
    "CertificateName": [
      "1.pdf",
      "2.pdf"
    ]
  },
  "5": {
    "LeaveEmpId": "rajeshk",
    "LeaveName": "Rajesh",
    "LeaveId": 3,
    "LeaveType": "annualleave",
    "LeaveTotal": 7,
    "LeaveDays": 1,
    "LeaveFrom": "2016-12-12T08:00:00+08:00",
    "LeaveTo": "2016-12-12T08:00:00+08:00",
    "LeaveAppliedDate": "2016-11-21T09:49:18.702+08:00",
    "LeaveStatus": "Processing",
    "CertificateName": [
      "1.pdf",
      "2.pdf"
    ]
  },
  "6": {
    "LeaveEmpId": "india",
    "LeaveName": "rahul",
    "LeaveId": 2,
    "LeaveType": "nopayleave",
    "LeaveTotal": 8,
    "LeaveDays": 1,
    "LeaveFrom": "2016-12-12T08:00:00+08:00",
    "LeaveTo": "2016-12-12T08:00:00+08:00",
    "LeaveAppliedDate": "2016-11-21T09:30:14.99+08:00",
    "LeaveStatus": "Processing",
    "CertificateName": [
      "1.pdf",
      "2.pdf"
    ]
  },
  "7": {
    "LeaveEmpId": "rajeshk",
    "LeaveName": "Rajesh",
    "LeaveId": 1,
    "LeaveType": "medicalleave",
    "LeaveTotal": 9,
    "LeaveDays": 1,
    "LeaveFrom": "2016-12-12T08:00:00+08:00",
    "LeaveTo": "2016-12-12T08:00:00+08:00",
    "LeaveAppliedDate": "2016-11-21T09:28:02.785+08:00",
    "LeaveStatus": "Processing",
    "CertificateName": [
      "1.pdf",
      "2.pdf"
    ]
 }
Run Code Online (Sandbox Code Playgroud)

在数据库中,我的Empid只是我使用的索引,它是唯一的,其他值不是唯一的。假ID仅在假数组中是唯一的。有人可以帮助我如何有效地做到这一点。感谢任何帮助。谢谢。

添加的信息:

我的数据库结构如下所示...这只是一个员工的信息。像这样,我有很多员工文件。我需要获取所有这些员工文档的总请假详细信息,以应用日期和状态排序的分页限制为20。谢谢

{
    "_id" : ObjectId("58320cec720fed31a3243683"),
    "name" : "Rajesh",
    "empid" : "rajeshk",
    "password" : "c1e1a43a3d65ff4d669f2831b9908acb9e2f755c48280e99f0d7a3e92592fa29",
    "phonenumber" : 0,
    "emailaddress" : "example@example.com.sg",
    "position" : "string",
    "accesslevel" : "string",
    "gender" : "string",
    "maritalstatus" : "string",
    "nationality" : "india",
    "department" : "it",
    "icnumber" : "string",
    "childcitizenship" : "string",
    "joindate" : ISODate("2015-11-21T00:00:00.000Z"),
    "confirmationdate" : Date(-62135596800000),
    "enddate" : Date(-62135596800000),
    "totalchild" : 0,
    "annualleave" : [ 
        {

            "id" : 0,
            "days" : 0.0,
            "type" : "",
            "from" : Date(-62135596800000),
            "to" : Date(-62135596800000),
            "applieddate" : Date(-62135596800000),
            "status" : "",
            "approveddate" : Date(-62135596800000),
            "certificate" : []
        }
    ],
    "medicalleave" : [ 
        {

            "id" : 0,
            "days" : 0.0,
            "type" : "",
            "from" : Date(-62135596800000),
            "to" : Date(-62135596800000),
            "applieddate" : Date(-62135596800000),
            "status" : "",
            "approveddate" : Date(-62135596800000),
            "certificate" : []
        }
    ],
    "nopayleave" : [ 
        {
            "total" : 10.0,
            "id" : 0,
            "days" : 0.0,
            "type" : "",
            "from" : Date(-62135596800000),
            "to" : Date(-62135596800000),
            "applieddate" : Date(-62135596800000),
            "status" : "",
            "approveddate" : Date(-62135596800000),
            "certificate" : []
        }, 
        {
            "total" : 9.0,
            "id" : 1,
            "days" : 1.0,
            "mdays" : 0.0,
            "type" : "nopayleave",
            "daytype" : "FullDay",
            "from" : ISODate("2016-12-12T00:00:00.000Z"),
            "to" : ISODate("2016-12-12T00:00:00.000Z"),
            "applieddate" : ISODate("2016-11-21T01:28:02.785Z"),
            "status" : "Processing",
            "approveddate" : Date(-62135596800000),
            "certificatename" : "",
            "certificate" : [ 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }, 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }
            ]
        }, 
        {
            "total" : 8.0,
            "id" : 2,
            "days" : 1.0,
            "mdays" : 0.0,
            "type" : "nopayleave",
            "daytype" : "FullDay",
            "from" : ISODate("2016-12-12T00:00:00.000Z"),
            "to" : ISODate("2016-12-12T00:00:00.000Z"),
            "applieddate" : ISODate("2016-11-21T01:30:14.990Z"),
            "status" : "Processing",
            "approveddate" : Date(-62135596800000),
            "certificatename" : "",
            "certificate" : [ 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }, 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }
            ]
        }, 
        {
            "total" : 7.0,
            "id" : 3,
            "days" : 1.0,
            "mdays" : 0.0,
            "type" : "nopayleave",
            "daytype" : "FullDay",
            "from" : ISODate("2016-12-12T00:00:00.000Z"),
            "to" : ISODate("2016-12-12T00:00:00.000Z"),
            "applieddate" : ISODate("2016-11-21T01:49:18.702Z"),
            "status" : "Processing",
            "approveddate" : Date(-62135596800000),
            "certificatename" : "",
            "certificate" : [ 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }, 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }
            ]
        }, 
        {
            "total" : 6.0,
            "id" : 4,
            "days" : 1.0,
            "mdays" : 0.0,
            "type" : "nopayleave",
            "daytype" : "FullDay",
            "from" : ISODate("2016-12-12T00:00:00.000Z"),
            "to" : ISODate("2016-12-12T00:00:00.000Z"),
            "applieddate" : ISODate("2016-11-21T01:52:20.185Z"),
            "status" : "Processing",
            "approveddate" : Date(-62135596800000),
            "certificatename" : "",
            "certificate" : [ 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }, 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }
            ]
        }, 
        {
            "total" : 5.0,
            "id" : 5,
            "days" : 1.0,
            "mdays" : 0.0,
            "type" : "nopayleave",
            "daytype" : "FullDay",
            "from" : ISODate("2016-12-12T00:00:00.000Z"),
            "to" : ISODate("2016-12-12T00:00:00.000Z"),
            "applieddate" : ISODate("2016-11-21T02:02:17.565Z"),
            "status" : "Processing",
            "approveddate" : Date(-62135596800000),
            "certificatename" : "",
            "certificate" : [ 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }, 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }
            ]
        }, 
        {
            "total" : 4.0,
            "id" : 6,
            "days" : 1.0,
            "mdays" : 0.0,
            "type" : "nopayleave",
            "daytype" : "FullDay",
            "from" : ISODate("2016-12-12T00:00:00.000Z"),
            "to" : ISODate("2016-12-12T00:00:00.000Z"),
            "applieddate" : ISODate("2016-11-21T02:11:58.283Z"),
            "status" : "Processing",
            "approveddate" : Date(-62135596800000),
            "certificatename" : "",
            "certificate" : [ 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }, 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }
            ]
        }, 
        {
            "total" : 3.0,
            "id" : 7,
            "days" : 1.0,
            "mdays" : 0.0,
            "type" : "nopayleave",
            "daytype" : "FullDay",
            "from" : ISODate("2016-12-12T00:00:00.000Z"),
            "to" : ISODate("2016-12-12T00:00:00.000Z"),
            "applieddate" : ISODate("2016-11-21T02:27:06.120Z"),
            "status" : "Processing",
            "approveddate" : Date(-62135596800000),
            "certificatename" : "",
            "certificate" : [ 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }, 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }
            ]
        }, 
        {
            "total" : 2.0,
            "id" : 8,
            "days" : 1.0,
            "mdays" : 0.0,
            "type" : "nopayleave",
            "daytype" : "FullDay",
            "from" : ISODate("2016-12-12T00:00:00.000Z"),
            "to" : ISODate("2016-12-12T00:00:00.000Z"),
            "applieddate" : ISODate("2016-11-21T02:29:33.713Z"),
            "status" : "Processing",
            "approveddate" : Date(-62135596800000),
            "certificatename" : "",
            "certificate" : [ 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }, 
                {
                    "filename" : "Rajesh",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }
            ]
        }
    ],
    "childcareleave" : [ 
        {
            "total" : 10.0,
            "id" : 0,
            "days" : 0.0,
            "type" : "",
            "from" : Date(-62135596800000),
            "to" : Date(-62135596800000),
            "applieddate" : Date(-62135596800000),
            "status" : "",
            "approveddate" : Date(-62135596800000),
            "certificate" : []
        }
    ],
    "hospitalleave" : [ 
        {
            "total" : 10.0,
            "id" : 0,
            "days" : 0.0,
            "mdays" : 0.0,
            "type" : "",
            "daytype" : "",
            "from" : Date(-62135596800000),
            "to" : Date(-62135596800000),
            "applieddate" : Date(-62135596800000),
            "status" : "",
            "approveddate" : Date(-62135596800000),
            "certificatename" : "string",
            "certificate" : []
        }
    ],
    "maternityleave" : [ 
        {
            "total" : 10.0,
            "id" : 0,
            "days" : 0.0,
            "mdays" : 0.0,
            "type" : "",
            "daytype" : "",
            "from" : Date(-62135596800000),
            "to" : Date(-62135596800000),
            "applieddate" : Date(-62135596800000),
            "status" : "",
            "approveddate" : Date(-62135596800000),
            "certificate" : []
        }
    ],
    "paternityleave" : [ 
        {
            "total" : 0.0,
            "id" : 0,
            "days" : 0.0,
            "mdays" : 0.0,
            "type" : "",
            "daytype" : "",
            "from" : Date(-62135596800000),
            "to" : Date(-62135596800000),
            "applieddate" : Date(-62135596800000),
            "status" : "",
            "approveddate" : Date(-62135596800000),
            "certificate" : []
        }
    ],
    "compassionateleave" : [ 
        {
            "total" : 0.0,
            "id" : 0,
            "days" : 0.0,
            "type" : "",
            "from" : Date(-62135596800000),
            "to" : Date(-62135596800000),
            "applieddate" : Date(-62135596800000),
            "status" : "",
            "approveddate" : Date(-62135596800000),
            "certificate" : []
        }
    ],
    "marriageleave" : [ 
        {
            "total" : 0.0,
            "id" : 0,
            "days" : 0.0,
            "type" : "",
            "from" : Date(-62135596800000),
            "to" : Date(-62135596800000),
            "applieddate" : Date(-62135596800000),
            "status" : "",
            "approveddate" : Date(-62135596800000),
            "certificate" : []
        }
    ],
    "otherleave" : []
}
Run Code Online (Sandbox Code Playgroud)

icz*_*cza 5

照原样,您无法真正实现更有效的分页,因为您要列出和分页的所有文档都在一个文档中,分为多个切片/数组。

为了实现高效而简单的分页,我将重构您的模型。首先,我将所有类型的XXLeave文档存储为相同的类型,Leave然后向其中添加一个属性,该属性可以告诉您它的休假类型(例如Annual,Medical等等)。接下来,我不会将此列表Employee本身存储,而是存储在单独的MongoDB集合中,并且它可以具有一个EmployeeID字段,告诉Leave文档属于哪个员工(所有者)。

有了这种模型,您可以Leave通过查询轻松列出员工的特定类型的文档:

c := session.DB("").C("leaves")
err := c.Find(bson.M{"empid": eid, "type": LeaveAnnual}).All(&result)
Run Code Online (Sandbox Code Playgroud)

要列出Leave员工的所有文件,只需不使用"type"过滤器:

err := c.Find(bson.M{"empid": eid}).All(&result)
Run Code Online (Sandbox Code Playgroud)

现在开始分页。如果您有此模型,那么分页就成了小菜一碟:

q = c.Find(bson.M{"empid": eid}).Sort("applieddate", "status")
// Use Skip() and Limit() to denote the page you want to send, e.g.
q = Skip((page-1) * 20).Limit(20)
err := q.All(&result)
Run Code Online (Sandbox Code Playgroud)

如果要发送所有Leave文档并对其进行分页,而不论Employee作者(作者)如何,那么也请忽略EmpId过滤器:

err := c.Find(nil).Sort("applieddate", "status").
    Skip((page-1) * 20).Limit(20).All(&result)
Run Code Online (Sandbox Code Playgroud)

请注意,使用Skip()和Limit()适用于“少量”文档。但是对于大量文档,应基于指定起始索引条目使用分页。有关更多详细信息,请参见此问题,并为其提供一个现成的库:

使用mgo在MongoDB中进行有效的分页