首页 > 解决方案 > 在 Node.js 中使用 Mongoose 进行多查询/多排序(?)

问题描述

我一直在一个新闻网站上工作,用户可以在该网站上发表文章并发表评论。部分评论是为评论指定一种情绪(5 个选项之一:惊讶、快乐、中性、悲伤、愤怒)。

我想要做的就是将特定情绪的情绪计数最高的文章列为“前 5 名”。当然,我知道在论坛上情绪总是“愤怒”,所以我应该与它一起工作;D,但这是学习经历的一部分,我想知道如何做到这一点。

因此,对于其他 top5(“热门讨论”和“热门新闻”),我在我的文章架构中引入了计数器,当发表评论时(用于“hotDiscussion”)或当用户将某些内容指定为真正的“热门”时,计数器会增加新闻”(“喜欢按钮功能”)。然后我只需搜索文章并按这些计数器排序...

但是,对于这种特定情况,有 5 个选项,我该如何排序?!?...起初我以为我会在选择某种情绪时实现计数器(下拉菜单)。但是,所有的计数器都作为一个单一的实体“在文章上”,我不明白你会如何对所有计数器进行排序以获得一个全面的列表。所以然后我想把它们放在一个对象或数组中......就像:

Article {
   title: String,
   author: user...
   comments: []
   emotions: [ or {
        Amazed: amazedCount,
        Happy: happyCount,
        Neutral: neutralCount,
        Sad: sadCount,
        Angry: angryCount
   } or ]

然后,也许我可以找到所有文章并按情绪排序(不知何故?),然后从列表中浏览前 5 名。但在这里我又遇到了 5 个选项的事情。我不知道如何对情绪进行分类?然后也许在结果中,按第一个(最高)计数排序。然后略过top5。

所以在这里环顾四周有点暗示我正在做这样的事情

Article {
   ...
   ...
   emotions: [ 
       {name: Amazed, value: amazedCount},
       {name: Happy, value: happyCount},
       {name: Neutral, value: neutralCount},
       {name: Sad, value: sadCount},
       {name: Angry, value: angryCount}
     ]

我有点喜欢这个方向,因为“名称”和“值”键很清晰。但是在这里我遇到了同样的问题,又名。缺乏知识......因为:

所以希望有人可以帮助我,因为我可以看到这种类型的搜索在各种类型的情况下都很有价值,而不仅仅是对于某些学习环境类型的“论坛”top5。


对于那些需要更有用的“用例”的人

warehouse{
   name: String,
   ... : ...
   parts: [
        {name: part1, value: Count}
        {name: part2, value: Count}
        {etc }
         ]
    }

根据零件数量生成仓库列表(重复提及某个仓库不是问题);或者说一个可用品牌或型号数量最多的汽车经销商列表(以获得重新分配 f/e 的输入)。或具有多价值评级系统的在线商店列表(f / e:交货速度,客户服务,产品价格等)

标签: node.jssortingmongoose

解决方案


好吧,事实证明这实际上相当简单......因为我最喜欢那里。只需将排序标准放在 '...' /doh 之间!

这段代码不完整,就像复制/粘贴一样,你已经完成了,但下面的代码确实向你展示了你需要“做”事情以使其工作的大部分地方(/显示不包括的路线,也就是用户看到的页面并且可以输入东西)

    // 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*
    
        });
    });

希望这对将来某个地方的人有所帮助:)


推荐阅读