Gunakan alur agregasi berikut:
db.click_log.aggregate([
{ "$match" : { "log.type" : { "$ne" : "emailed" } } }, // get rid of docs with an "emailed" value in log.type and docs not from this month
{ "$unwind" : "$log" }, // unwind to get log elements as separate docs
{ "$project" : { "_id" : 1, "log" : 1, "month" : { "$month" : "$log.utc_timestamp" } } },
{ "$match" : { "log" : "clicked", "month" : <# of month> } }, // get rid of log elements not from this month and that aren't type clicked
{ "$group" : { "_id" : "$_id", "count" : { "$sum" : 1 } } } // collect clicked elements from same original doc and count number
])
Ini akan kembali, untuk setiap dokumen yang tidak memiliki "email" sebagai nilai log.type , jumlah elemen larik log yang memiliki log.type nilai clicked dan dengan stempel waktu dari bulan berjalan. Jika Anda menginginkan periode geser 30 hari selama sebulan, ubah $match menjadi kueri rentang dengan $gt dan $lt mencakup periode waktu yang diinginkan.