我一直在一个新闻网站上工作,用户可以在那里发表文章和留言。评论的一部分是为评论指定一种情感(5种选择之一:惊讶、快乐、中立、悲伤、愤怒)。
我想要做的是让一篇情绪最高的文章中的“前5”对一种特定的情绪起重要作用。当然,我知道在一个论坛上,人们的情绪总是“愤怒”的,所以我应该处理这个问题;D,但这是学习经验的一部分,我想知道如何做到这一点。
因此,对于其他前5名(“热门讨论”和“热门新闻”),我在我的文章Schema中引入了计数器,无论是在注释(用于“hotDiscussion”)时,还是当用户将某个内容指定为真正的“热门新闻”(“类似按钮功能”)时,这些计数器都会增加。然后我只需搜索这些文章并按这些柜台排序.
但是,对于这个特殊的案例,有5个选项,我该如何分类呢?一开始,我想当我选择某种情绪(下拉菜单)时,我只会执行计数器。但是,所有的计数器都是“在文章上”作为一个单一的实体,我看不出你会如何按照它们来排序,以获得一个全面的清单。所以我想把它们放在一个物体或数组里.类似于:
Article {
title: String,
author: user...
comments: []
emotions: [ or {
Amazed: amazedCount,
Happy: happyCount,
Neutral: neutralCount,
Sad: sadCount,
Angry: angryCount
} or ]然后,也许我可以找到所有的文章,并按情绪排序(不知怎么的?)然后从名单上略去前五名。但在这里,我再次遇到了5种选择。我不知道该怎么整理情绪?到那时,也许在结果中,按第一个(最高)计数排序。然后浏览一下top5。
所以看看这里多少暗示着我要做这样的事情
Article {
...
...
emotions: [
{name: Amazed, value: amazedCount},
{name: Happy, value: happyCount},
{name: Neutral, value: neutralCount},
{name: Sad, value: sadCount},
{name: Angry, value: angryCount}
]我有点喜欢这个方向,因为清晰的键 'name‘& 'value’。但在这里,我遇到了同样的问题,又名。缺乏知识..。因为:
因此,希望有人能帮助我,因为我可以看到这种类型的搜索在不同类型的情况下很有价值,而不仅仅是一些学习环境类型的‘论坛’top5。
对于那些需要更有用的“用例”的人来说
warehouse{
name: String,
... : ...
parts: [
{name: part1, value: Count}
{name: part2, value: Count}
{etc }
]
}根据零件数量生成一个仓库列表(在这个列表中,重复提到某个仓库并不是一个问题);或者说一个拥有最高数量的现有品牌或型号的汽车经销商列表(以获得重新分配f/e的投入)。或具有多价值评级系统的在线商店列表(f/e:交货速度、客户服务、产品价格等)
发布于 2020-05-29 20:54:41
其实这很简单..。因为我是去那里的路。只需要将排序标准放在“.”/doh之间!
! 这段代码不是完整的,就像在复制/粘贴中所做的那样,但是下面的代码确实显示了您需要在哪些地方“做”事情才能使它工作(/show路由不包括,也就是用户看到并可以输入的页面)。
// In the Schemas:
Article {
...
...
emotions: [ {emotion: String, eCount: Number} ]
}
Comment{
emotion: String; //to save the emotion of the user on the comment
}
// Upon initializing an Article:
router.post("/route", function(req, res){
let etcetera: etc,
let theEmotions = [ // in your usecase you may populate these elsewhere
{ emotion: "Amazed", eCount: 0 },
{ emotion: "Happy", eCount: 0 },
{ emotion: "Neutral", eCount: 0 },
{ emotion: "Sad", eCount: 0 },
{ emotion: "Angry", eCount: 0 }
];
let newArticle = {
etc: etc,
emotions: theEmotions
};
Article.create(newArticle, function(err, newArticle){
*stuff here*
});
});
// when a comment gets made, it updates the scores based upon the emotion selected
//REST :: CREATE --> actually creates a new comment
router.post("/", function(req, res){
Article.findById(req.params.id, function(err, foundArticle){
if(err){
console.log("ERROR @ finding article to POST the COMMENT");
res.redirect('/articles');
} else {
Comment.create(req.body.comment, function(err, createdComment){
if(err){
console.log("ERROR @ CREATing new COMMENT");
} else {
switch(createdComment.emotion){
case "Amazed":
foundArticle.emotions[0].eCount += 1;
break;
case "Happy":
foundArticle.emotions[1].eCount += 1;
break;
case "Neutral":
foundArticle.emotions[2].eCount += 1;
break;
case "Sad":
foundArticle.emotions[3].eCount += 1;
break;
case "Angry":
foundArticle.emotions[4].eCount += 1;
break;
default:
console.log("ERROR @ Emotion Switch in comments route");
}
//add username and id to comment
createdComment.user.id = req.user._id;
createdComment.user.username = req.user.username;
createdComment.save();
foundArticle.comments.push(createdComment);
foundArticle.save();
console.log("SUCCESS @ CREATing new COMMENT");
res.redirect('/articles/' + foundArticle._id);
}
});
}
});
});
//Where one wants to sort: ... note the '...' around 'emotions.eCount'
router.get("/specificRoute, function(req, res){
Article.find({}).sort({'emotions.eCount': -1}).exec(function(err, hotEmotions){
*stuff happens here*
});
});希望这对将来某个地方的人有所帮助:)
https://stackoverflow.com/questions/62031830
复制相似问题