Hello everyone, we have a curious situation with sustained queries running between our server (on Digital Ocean) and our database (on Mongo Atlas) that seems to only occur when we run aggregate queries through tunguska-reactive-aggregate.
(Sorry this is a long description. I wanted to put the facts out as best as I could to help give context)
We have an admin portion of the app that queries for the first 10 records of a collection and waits for the user to select a record, ask for the next 10, sort or filter. Typical paginated usage. On the server, we assemble an aggregate query (using tunguska-reactive-aggregate) to assemble things from other collections that will help a user find things in the current collection. Things like the user’s email that is not available in the current collection.
What happens is that while the user is looking at these first 10 records, Atlas is showing considerable, sustained activity (“internet”) and Digital Ocean is likewise showing this activity coming in, but no activity going out. On the client side, minimongo shows the 10 records and the total bytes transferred is correct for these 10 records
If the user leaves this page up, no further minimongo activity is recorded (as expected) but Digital Ocean continues to be receiving a sustained traffic of data from Atlas. If the user leaves the window open for very long they are getting billed the whole time. So far that has reached $40 but that could get well over $300 at the rate Atlas charges. So far we/they have been fortunate.
Once the user selects a record or leaves the page, the activity between Atlas and Digital Ocean ceases, so it seems that this is a publication issue and it gets extinguished when the subscription ends. Should a second admin user open the same page and leave it on their browser, the traffic does NOT increase at all. If that admin users goes to the listing page of a different collection, then they DO add to the traffic. The amount of added usage increases with the total # of records in the collection. These are hopefully useful pieces of the puzzle.
So my question is this: do reactive aggregate queries behave this way (is this expected behaviour) or have we created a poor aggregate query?
The query is assembled incrementally, but here is an example of the first one sent when the subscription starts. I’m hoping someone has seen this issue before or perhaps sees what blunder I made in the query itself?
Here’s the query:
[
{
"$match": {}
},
{
"$unwind": {
"path": "$products",
"preserveNullAndEmptyArrays": true
}
},
{
"$sort": {
"products.paid": -1,
"products.publishDate": -1
}
},
{
"$addFields": {
"ttaCount": {
"$size": {
"$ifNull": [
"$products.children",
[]
]
}
}
}
},
{
"$group": {
"_id": "$_id",
"_version": {
"$first": "$_version"
},
"createdAt": {
"$first": "$createdAt"
},
"updatedAt": {
"$first": "$updatedAt"
},
"category": {
"$first": "$category"
},
"duration": {
"$first": "$duration"
},
"logoImageId": {
"$first": "$logoImageId"
},
"institution": {
"$first": "$institution"
},
"country": {
"$first": "$country"
},
"region": {
"$first": "$region"
},
"city": {
"$first": "$city"
},
"title": {
"$first": "$title"
},
"applicationUrl": {
"$first": "$applicationUrl"
},
"jobBody": {
"$first": "$jobBody"
},
"userId": {
"$first": "$userId"
},
"isPublished": {
"$first": "$isPublished"
},
"preventPublishOverride": {
"$first": "$preventPublishOverride"
},
"bannerImageId": {
"$first": "$bannerImageId"
},
"slug": {
"$first": "$slug"
},
"currentListing": {
"$first": "$products"
},
"ttaCount": {
"$sum": "$ttaCount"
}
}
},
{
"$lookup": {
"from": "userProfiles",
"localField": "userId",
"foreignField": "_id",
"as": "user"
}
},
{
"$unwind": {
"path": "$user",
"preserveNullAndEmptyArrays": true
}
},
{
"$addFields": {
"live": {
"$and": [
{
"$eq": [
"$isPublished",
true
]
},
{
"$eq": [
"$preventPublishOverride",
false
]
}
]
}
}
},
{
"$project": {
"_id": 1,
"_version": 1,
"createdAt": 1,
"updatedAt": 1,
"category": 1,
"duration": 1,
"logoImageId": 1,
"institution": 1,
"country": 1,
"region": 1,
"city": 1,
"title": 1,
"applicationUrl": 1,
"products": 1,
"jobBody": 1,
"userId": 1,
"isPublished": 1,
"preventPublishOverride": 1,
"bannerImageId": 1,
"slug": 1,
"live": 1,
"ttaCount": 1,
"user.email": 1,
"currentListing": 1
}
},
{
"$match": {}
},
{
"$sort": {
"updatedAt": -1
}
},
{
"$skip": 0
},
{
"$limit": 10
}
]```