Answer the question
In order to leave comments, you need to log in
How to more correctly make a selection to view popular posts for a certain period?
The task was to make a selection of views per day, week and month, to do what I did and it works, but I think it can be done better, so I want to listen to the advice of those who have already done it or those who have experienced it.
In order to implement this, I had to make a new collection Counts
Scheme mongoose
var schema = new Schema({
href: { type: String }, // URL document.url
count: { type: Number, default: 1 }, // вообще 1, но тут и фейковые просмотры (random 1-5)
createdAt: { type: Date, default: Date.now }
})
var schema = new Schema({
url: { type: String }, // URL document.url
createdAt: { type: Date, default: Date.now }
})
return Counts.aggregate([
{
$match: { createdAt: { $gte: new Date(n.setDate(n.getDate() - 1 )) } }
}, {
$project: {
_id: 1,
href: 1,
count: 1
}
}, {
$group: {
_id: "$href",
count: {
$sum: "$count"
}
}
}, {
$sort: { count: -1 }
}, {
$skip: request.skip
}, {
$limit: 12
}
])
[
{
_id: "Documents.url",
count: 5000
}
//... и так 12 записей
]
return Documents
.aggregate([
{ $match: { url: { $in: ["Counts.href","Counts.href", "Counts.href", "Counts.href"] } } },
{
$project: {
_id: 1,
url: 1,
text: 1,
// тут еще куча всего
}, {
$group: {
_id: "$_id",
url: { $first: "$url" },
text: { $first: '$text' }
}
}
])
.then(e => {
return Promise.all(e.map(async document => {
return Promise.all(request.itemsCounts.map(async count => {
if(document.url === count._id) return document.count = count.count
})).then(e => {
return document
})
}))
})
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