I have two collections: User and Tweets. The schemas are like these (I removed the fields that are not important, since they are a lot):
UsersSchema = {
_id:Number,
followers_count:Number
};
TweetsSchema = {
_id:Number,
is_retweet:Boolean,
user:{type:Number, ref:"Users"}
}
When I query Tweets, I usually make a populate("user")
so I know who was the user who made the tweet. So far so good. But now, I need to retrieve the number of followers (followers_count) of all the tweets that are a retweet (is_retweet). I have tried with aggregations, but I cannot use a parameter that has not been created in the Tweets schema (followers_count) to make a sum.
I don't have much experience in Mongo :( and I have searched a while for an answer... is there any way to solve this query or do I have to make first a query of all the user ids that appear in the tweets with is_retweet and then another query that returns the followers_count of that array of ids?
MongoDB does not do joins. If you need pieces of information available in two collections, you need at least two queries.
Depending on exactly what your "typical" query is, you should consider denormalizing some or all user information into a tweet.
There's also a semantic issue with your query. Which followers count do you want? The number of followers of the tweeter at the time they tweeted? Their followers at the time when you pulled their user document? For retweets, it might be more relevant to know how many followers they had at the time of the tweet that was eventually retweeted. You can imagine a social media personality starting with 8 followers, tweeting 140 characters of crystallized brilliance, and through popularity gained by retweets having 150000000 followers two months later.