需要使用 mgo 投影嵌套数组
need to project nested array using mgo
我使用 go lang 作为我的应用程序的后端,mongoDB 作为 database.I 我面临一个问题,我比较名称并投影 leave 数组和我也需要的内部要为 leave.Since 投影证书,我只需要来自我想使用管道和项目实现的员工结构的少量信息。
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",
},
},
})
这里的问题是我不知道如何检索与该特定相关的证书的所有信息leave.Please请注意,该证书是一个数组,其中至少有两个索引...
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 是否有任何其他方法可以做到 this.Appreciate 任何 help.Please.Thanks
使用上面的文档结构示例,您只需通过 $project
公开 certificate
,如下所示:
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)
由于您已经在 Leave
上使用了 $unwind
,因此每个请假申请都有单独的文件。上述聚合的输出为:
{
"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
}
]
}
其中每个请假申请都有自己与请假相关联的相应证书。
我使用 go lang 作为我的应用程序的后端,mongoDB 作为 database.I 我面临一个问题,我比较名称并投影 leave 数组和我也需要的内部要为 leave.Since 投影证书,我只需要来自我想使用管道和项目实现的员工结构的少量信息。
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",
},
},
})
这里的问题是我不知道如何检索与该特定相关的证书的所有信息leave.Please请注意,该证书是一个数组,其中至少有两个索引...
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 是否有任何其他方法可以做到 this.Appreciate 任何 help.Please.Thanks
使用上面的文档结构示例,您只需通过 $project
公开 certificate
,如下所示:
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)
由于您已经在 Leave
上使用了 $unwind
,因此每个请假申请都有单独的文件。上述聚合的输出为:
{
"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
}
]
}
其中每个请假申请都有自己与请假相关联的相应证书。