Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从MongoDB获取不同的值_Node.js_Mongodb_Pug - Fatal编程技术网

Node.js 从MongoDB获取不同的值

Node.js 从MongoDB获取不同的值,node.js,mongodb,pug,Node.js,Mongodb,Pug,我在我的MongoDB集合中有以下数据记录 { "_id": { "8uk4f9653fc4gg04dd7ab3d3"}, "title": "my title", "url": "https://myurl/entry/1ethd485", "author": "john", "created": { "2020-05-20T08:25:47.438Z"}, "vote": 1619 }, { "_id": { "6fd4fgh53fc4

我在我的
MongoDB
集合中有以下数据记录

{
    "_id": { "8uk4f9653fc4gg04dd7ab3d3"},
    "title": "my title",
    "url": "https://myurl/entry/1ethd485",
    "author": "john",
    "created": { "2020-05-20T08:25:47.438Z"},
    "vote": 1619
},
{
    "_id": { "6fd4fgh53fc4gg04dd7gt56d"},
    "title": "my title",
    "url": "https://myurl/entry/1ethd485",
    "author": "john",
    "created": { "2020-05-19T04:12:47.457Z"},
    "vote": 1230
}
如您所见,标题url作者可以相同,但\u id创建的投票始终是唯一的

这是我在
app.js
中的
路线

// Home Route
app.get('/', function(req, res){
  Entry.find({}).sort({ "vote" : -1 }).limit(500).exec(function(err, entries){
    if(err){
      console.log(err);
    } else {
      res.render('index', {
        entries: entries
      });
    }
  });
});
此路由按投票值的降序显示500条记录。这将显示上面两个投票值为1619和1230的记录。然而,我想要实现的是只显示相同标题、url和作者的最大投票值。在本例中,它应仅显示投票值为1619的记录。最好的方法是什么?这里正确的使用方法是什么

仅供参考,这是我的哈巴狗布局图

extends layout
    block content
      h1 #{title}
      ul.list-group
        each entry, i in entries
          li.list-group-item
            a(href=entry.url)= entry.title
app.get(“/”,(req,res)=>{
聚合([{$group:{$id:{$author],author:{$first:$author”},投票:{$max:$vote”},url:{$first:$url”},标题:{$first:$title”},{$sort:{vote:-1},{$limit:500}])。执行((错误,结果)=>{
如果(错误){
失误
}
res.send(结果)
})
})

应用程序获取(“/”,(请求,请求)=>{
聚合([{$group:{$id:{$author],author:{$first:$author”},投票:{$max:$vote”},url:{$first:$url”},标题:{$first:$title”},{$sort:{vote:-1},{$limit:500}])。执行((错误,结果)=>{
如果(错误){
失误
}
res.send(结果)
})
})


请您详细说明一下。这如何符合我的“Entry.find({}).sort({“vote”:-1}).limit(500)”标准。我仍然想使用它。你可以使用这个
条目.distinct(“author”).exec((err,results))
这个方法的问题是它也会返回最小值的对象。但在上面发生的是,它将选择相同的复选框,并选择一个具有最大投票数的不同复选框。我想您希望检索max vote和unique author对象。实际上,我希望max vote和unique author、title和url一起检索。此方法将检索author的整个对象,标题和URL我已经为您做了更改,并在下面发布了代码。在您的应用程序中尝试一下,您会得到结果。请详细说明。这如何符合我的“Entry.find({}).sort({“vote”:-1}).limit(500)”标准。我仍然想使用它。你可以使用这个
条目.distinct(“author”).exec((err,results))
这个方法的问题是它也会返回最小值的对象。但在上面发生的是,它将选择相同的复选框,并选择一个具有最大投票数的不同复选框。我想您希望检索max vote和unique author对象。实际上,我希望max vote和unique author、title和url一起检索。此方法将检索author的整个对象,标题和URL我已经为您做了更改,并在下面发布了代码。在您的应用程序中尝试一下,您将得到结果。这似乎带来了结果,但它不是按投票降序排序。您也可以按投票降序排序
{$sort:{vote:-1}}
在聚合函数中应用此对象。我已经编辑了答案,您可以尝试。现在我已经对答案进行了修改,这肯定会起作用。我也增加了限制,你可以尝试一下。这似乎带来了结果,但它不会按投票降序排序。你也可以按投票降序排序
{$sort:{vote:-1}}
在聚合函数中应用此对象。我已经编辑了答案,您可以尝试。现在我对答案做了修改,这肯定会奏效。我也增加了限制,你可以试试
if you have to find out distinct along with maximum value the you can use the following command:

`Entry.aggregate([{$group:{_id:"$author",votevalue:{$max:"$vote"}}}]);`