Answer the question
In order to leave comments, you need to log in
How to correctly count views, etc. aggregation?
Hello.
Actually there are a lot of users like this:
_id: 123,
name: 'ivan',
...,
views: [{post_id: 456, timestamp: Date()}, {post_id: 789, timestamp: Date()}, ...],
likes: [{post_id: 789, timestamp: Date()}],
dislikes: [{post_id: 456, timestamp: Date()}]
There are, let's say, 10 articles on the page, I want to display for each article total number of unique views, likes, dislikes.
Through aggregation match by $in post_id and unwind views, I was able to get a unique number of views for each post on the page with one request, but then I came to a dead end, I couldn’t add likes and dislikes to this dataset.
How to get views / likes / dislikes for each post on the page and do it as "energy efficiently" and quickly as possible?
upd:
this way I can count the views of the posts I'm interested in.
db.users.aggregate( [
{ $match: { 'views.post_id': { $in: [1104, 1105] } } },
{ $project : { '_id': 0, 'views.post_id': 1, 'likes.post_id': 1, 'dislikes.post_id': 1 } },
{ $unwind: '$views' },
{ $match: { 'views.post_id': { $in: [1104, 1105] } } },
{ $group: {
_id: '$views.post_id',
views: { "$sum": 1 }
} }
] );
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question