简体   繁体   中英

how to query mongo find / aggregate, based on objects that are not arrays

I will look for data in rekap_rfid, and release all data based on year _2019, and can input custom year, month, and day

to be used as a monthly and annual recap I must take several values, such as year and date.

then data will appear based on the year and month that I input


 {
"_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" : "-"
}

}


desired output results

[
  {
    "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"
      }
    }]
  }
}]

so above the sample results I took all the data for _2019 in April and the 23rd

but if you find a better JSON structure to take the date and year in the results above I will use it

I am still not clear with your schema and your desired result. For example, you have multiple Objects(key-value pair) at key "April" which is not the right format and secondly, you are expecting array at key "April".

Apart from that, you are saving year, date and month at the key which you should save on value. For example :

"Year": "2019" ,
"month": "April",
"date": "23" 

Or you can refer to MongoDB Data modeling

But still you can try query as below:

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"  }
    }

])

The resulted response will be as below:

{
    "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"
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM