我将在rekap_rfid中查找数据,并根据_2019年发布所有数据,并可以输入自定义的年,月和日
要用作月度和年度回顾,我必须输入多个值,例如年份和日期。
然后数据将根据我输入的年份和月份显示
{
"_id" : ObjectId("5cc0273fae15393f68003c75"),
"email" : "admin@gmail.com",
"sandi" : "1a954924c5f82bd12d655b4c23cc8b84",
"peran" : "Murid",
"mengajar" : [],
"RFID" : {
"serial_number" : "-",
"status" : "",
"rekap_rfid" : {
"_2019" : {
"April" : {
"_23" : {
"Datang" : ISODate("2019-04-23T07:00:00.000Z"),
"Pulang" : ISODate("2019-04-23T12:00:00.000Z"),
"Status_kehadiran" : "hadir"
},
"_24" : {
"Datang" : "2019-04-24 05:28:07",
"Pulang" : "2019-04-24 05:28:23",
"Status_kehadiran" : "hadir"
},
"_25" : {
"Datang" : "2019-04-25 11:43:38",
"Pulang" : "2019-04-25T08:17:01.704Z",
"Status_kehadiran" : "hadir"
}
},
"Mei" : {
"_01" : {
"Datang" : ISODate("2019-05-01T07:00:00.000Z"),
"Pulang" : ISODate("2019-05-01T12:00:00.000Z"),
"Status_kehadiran" : "sakit"
}
}
}
}
},
"Kelas" : [
{
"nama_kelas" : "7A",
"tahun_ajaran" : "2018"
},
{
"nama_kelas" : "8A",
"tahun_ajaran" : "2019"
}
],
"sekolah" : "HighSchool Test",
"profil" : {
"username" : "admin",
"nama_lengkap" : "admin",
"jenis_kelamin" : "L",
"bio" : "-",
"foto" : "-"
}
}
所需的输出结果
[
{
"email":"admin@gmail.com",
"_2019":{
"April":[
{
"_23":{
"Datang":ISODate("2019-05-01T07:00:00.000Z"),
"Pulang":ISODate("2019-05-01T12:00:00.000Z"),
"Status_kehadiran":"hadir"
}
}
]
}
},
{
"email":"other_admin@gmail.com",
"_2019":{
"April": [{
"_23":
{
"Datang": ISODate("2019-05-01T08:00:00.000Z"),
"Pulang": ISODate("2019-05-01T13:50:00.000Z"),
"Status_kehadiran": "hadir"
}
}]
}
}]
因此在示例结果上方,我获取了 4月和 23rd
中 _2019 的所有数据但是,如果您在上面的结果中找到更好的JSON结构来记录日期和年份,我会使用
答案 0 :(得分:0)
我仍然不清楚您的架构和所需的结果。例如,您在键“ April”处有多个对象(键-值对),这是不正确的格式,其次,您期望键“ April”处有数组。
除此之外,您还保存了年,日期和月,因此应该节省价值。例如:
"Year": "2019" ,
"month": "April",
"date": "23"
或者您可以参考MongoDB Data modeling
但是您仍然可以尝试如下查询:
db.collection.aggregate([
{
$project: {
"RFID":{
"email": "$email",
"rekap_rfid": "$RFID.rekap_rfid"
}
}
},
{
$replaceRoot: { newRoot: "$RFID" }
},
{
$project: {
"data":{
"email": "$email",
"_2019": "$rekap_rfid._2019"
}
}
},
{
$replaceRoot: { newRoot: "$data" }
}
])
结果将如下所示:
{
"email" : "admin@gmail.com",
"_2019" : {
"April" : {
"_23" : {
"Datang" : ISODate("2019-04-23T12:30:00.000+05:30"),
"Pulang" : ISODate("2019-04-23T17:30:00.000+05:30"),
"Status_kehadiran" : "hadir"
},
"_24" : {
"Datang" : "2019-04-24 05:28:07",
"Pulang" : "2019-04-24 05:28:23",
"Status_kehadiran" : "hadir"
},
"_25" : {
"Datang" : "2019-04-25 11:43:38",
"Pulang" : "2019-04-25T08:17:01.704Z",
"Status_kehadiran" : "hadir"
}
},
"Mei" : {
"_01" : {
"Datang" : ISODate("2019-05-01T12:30:00.000+05:30"),
"Pulang" : ISODate("2019-05-01T17:30:00.000+05:30"),
"Status_kehadiran" : "sakit"
}
}
}
}