Hello Folks,
I have a profile collection where each document will have a birthDate & weddingDatefield ,so i want to calculate birthdays & weddingdays coming in 14 days ,to do so I use the below query, this query find birthdays & weddings for all documents in the collection but can I do the same thing in batches.
Example:
- Lets say if I have 1m documents in the profile collection and I want to calculate birthdays or weddingdays , then I want to do it in batches (1000 documents for each batch).
- For each 1000 documents we calculate weddingdays & birthdays.
- Until the total count.
Is caculating in the above method is a scalable one or doing it for all documents at once, and if there is any other pattern which is scalable it would be of great help.
Note:
- Using this query in a cron task to send push notifications to users regarding the upcoming events.
db.collection.profiles.aggregate([
{
$project: {
_id: 0,
fcmToken: 1,
userNumber: 1,
displayName: 1,
todayDayOfYear: { $dayOfYear: new Date() }, // this returns the current day in year ex:if today is feb 10,2022 => 41
birthDayOfYear: { $dayOfYear: "$birthDate" }, //this returns the day in year of user birthday,
weddingDayOfYear: { $dayOfYear: "$weddingDate" }, //this returns the day in year of user weddingDay
},
},
//Projecting profiles for calculating the daysTillBirthday or birtday in n days
{
$project: {
fcmToken: 1,
userNumber: 1,
displayName: 1,
daysTillBirthday: {
$subtract: [
{
$add: [
"$birthDayOfYear",
{
$cond: [
{ $lt: ["$birthDayOfYear", "$todayDayOfYear"] },
365,
0,
],
},
],
},
"$todayDayOfYear",
],
},
daysTillWeddingDay: {
$subtract: [
{
$add: [
"$weddingDayOfYear",
{
$cond: [
{ $lt: ["$weddingDayOfYear", "$todayDayOfYear"] },
365,
0,
],
},
],
},
"$weddingDayOfYear",
],
},
},
},
// Filtering the profiles which has birthdays in 14 days
{
$match: {
$or: [
{ daysTillBirthday: { $lt: 15 } },
{ daysTillWeddingDay: { $lt: 15 } },
],
},
},
])