需要使用mgo投影嵌套数组

时间:2016-11-08 03:36:11

标签: mongodb go mgo

我使用go lang作为我的应用程序的后端和mongoDB作为数据库。我面临一个问题,我比较名称和项目离开数组和内部我还需要为该离开项目证书。因为我只需要使用管道和项目实现的员工结构中的少量信息。

 type (
        Employee struct {
            Name               string
            Password           string
           EmailAddress       string
           Position           string
           Gender             string
           Nationality        string
           Department         string
           MaritalStatus      string
           Approvedby         string
           JoinDate           time.Time
           ConfirmationDate   time.Time
           EndDate            time.Time
            Leave             []*LeaveInfo  
        }
        LeaveInfo struct {
            Total        float64
            Id           int
            Days         float64
            From        time.Time
             To          time.Time  
            Status       string
            Certificate  []*CertificateInfo
        }
        CertificateInfo struct {
            FileName string
            FileType string
            FileSize int

        }

数据库结构如下

{
    "_id" : ObjectId("58213e14927a62f3cf04e05b"),
    "name" : "string",
    "password" : "string",
    "emailaddress" : "string",
    "position" : "string",
    "gender" : "string",
    "maritalstatus" : "string",
    "approvedby" : "string",
    "nationality" : "german",
    "department" : "account",
    "joindate" : ISODate("2016-09-19T00:00:00.000Z"),
    "confirmationdate" : Date(-62135596800000),
    "enddate" : Date(-62135596800000),
    "Leave" : [ 
        {
            "total" : 20.0,
            "id" : 0,
            "days" : 0.0,
            "type" : "",
            "from" : ISODate("2016-12-12T00:00:00.000Z"),
            "to" : ISODate("2016-12-12T00:00:00.000Z"),
            "status" : "",
            "certificate" : [
                    {
                    "filename" : "malaysia",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }, 
                {
                    "filename" : "singapore",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }
             ]
        }, 
        {
            "total" : 19.0,
            "id" : 1,
            "days" : 1.0,

            "from" : ISODate("2016-12-12T00:00:00.000Z"),
            "to" : ISODate("2016-12-12T00:00:00.000Z"),
            "applieddate" : ISODate("2016-11-08T02:53:38.902Z"),
            "status" : "Processing",
            "approveddate" : Date(-62135596800000),
            "certificate" : [ 
                {
                    "filename" : "germany",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }, 
                {
                    "filename" : "england",
                    "filetype" : ".zip",
                    "filesize" : 1234
                }
            ]
        }, 
        {
            "total" : 18.0,
            "id" : 2,
            "days" : 1.0,
            "mdays" : 0.0,
            "type" : "annualleave",
            "daytype" : "FullDay",
            "from" : ISODate("2016-12-12T00:00:00.000Z"),
            "to" : ISODate("2016-12-12T00:00:00.000Z"),
            "applieddate" : ISODate("2016-11-08T05:36:21.579Z"),
            "status" : "Processing",
            "approveddate" : Date(-62135596800000),
            "certificate" : [
                   {
                    "filename" : "india",
                    "filetype" : ".zip",
                    "filesize" : 1234
                   }, 
                   {
                    "filename" : "france",
                    "filetype" : ".zip",
                    "filesize" : 1234
                   }
                 ]
             }
         ]
     }

golang中的代码如下

pipe3 := c.Pipe([]bson.M{
            {
                "$match": bson.M{

                   "name":name
                 },
            },
            {
                "$unwind": "$leave",
            },

            {
                "$project": bson.M{
                    "_id":          false,

                    "name":         1,
                    "Id":           "$leave.id",
                    "Total":        "$leave.total",
                    "Days":         "$leave.days",
                    "Status":       "$leave.status",
                },
            },
       })

这里的问题是我不知道如何检索与该特定假期相关的证书的所有信息。请注意,证书是一个包含至少两个索引的数组......

I need an output similar to this. 
                    "name":        "John",
                    "Id":           "1",
                    "Total":        "10.0",
                    "Days":         "2.0",
                    "Status":       "Process",
                    "Certificate" : [
                      {
                     "filename":"certificate1",
                     "filesize":"size1"
                     },
                    {
                        "filename":"certificate2",
                        "filesize":"size2"
                     }
                      ]


                 "name":        "John",
                "Id":           "2",
                "Total":        "8.0",
                "Days":         "2.0",
                "Status":       "Process",
                "Certificate" : [
                  {
                 "filename":"certificate1",
                 "filesize":"size1"
                 },
                {
                    "filename":"certificate2",
                    "filesize":"size2"
                 }
                  ]

这是否可以使用project.or还有其他方法可以做到这一点。感谢任何帮助.Please.Thanks

1 个答案:

答案 0 :(得分:1)

使用上面的文档结构示例,您只需通过certificate公开$project,如下所示:

pipeline := []bson.M{
                {"$match": bson.M{"name":"string"}},
                {"$unwind": "$Leave"},
                {"$project": bson.M{
                        "_id":          false,
                        "name":         1,
                        "Id":           "$Leave.id",
                        "Total":        "$Leave.total",
                        "Days":         "$Leave.days",
                        "Status":       "$Leave.status",
                        "Certificate":  "$Leave.certificate"},
                },
           }
pipe := collection.Pipe(pipeline)
response := []bson.M{}
err = pipe.All(&response)

由于您已在$unwind上使用Leave,因此每个请假申请都有单独的文件。上述聚合的输出将是:

 {
  "name": "string",
  "Id": 0,
  "Total": 20,
  "Days": 0,
  "Status": "",
  "Certificate": [
    {
      "filename": "malaysia",
      "filetype": ".zip",
      "filesize": 1234
    },
    {
      "filename": "singapore",
      "filetype": ".zip",
      "filesize": 1234
    }
  ]
},
{
  "name": "string",
  "Id": 1,
  "Total": 19,
  "Days": 1,
  "Status": "Processing",
  "Certificate": [
    {
      "filename": "germany",
      "filetype": ".zip",
      "filesize": 1234
    },
    {
      "filename": "england",
      "filetype": ".zip",
      "filesize": 1234
    }
  ]
},
{
  "name": "string",
  "Id": 2,
  "Total": 18,
  "Days": 1,
  "Status": "Processing",
  "Certificate": [
    {
      "filename": "india",
      "filetype": ".zip",
      "filesize": 1234
    },
    {
      "filename": "france",
      "filetype": ".zip",
      "filesize": 1234
    }
  ]
}

每次请假时,都有自己相应的休假证书。

相关问题