简体   繁体   English

Mongodb Group 并从文档中获取其他字段

[英]Mongodb Group and get other fields from documents

update so Mohammad Faisal has the best solution.However it breaks when a new document is added lol!更新以便 Mohammad Faisal 拥有最佳解决方案。但是,当添加新文档时它会中断,哈哈! so i learned a lot from his code and modified it and it Works!所以我从他的代码中学到了很多东西并修改了它,它有效! =) the code is all the way in the bottom. =) 代码一直在底部。

But here's what i said.. So i have this document但这是我说的.. 所以我有这个文件

{"_id":"5ddea2e44eb407059828d740",
"projectname":"wdym",
"username":"easy",
"likes":0,
"link":["ssss"]
}


{"_id":"5ddea2e44eb407059822d740",
"projectname":"thechosenone",
"username":"easy",
"likes":30,
"link":["ssss"]
}

{"_id":"5ddea2e44eb407059828d740",
"projectname":"thanos",
"username":"wiley",
"likes":10,
"link":["ssss"]
}

and basically what i want is the document that contains the highest likes with it's associated project name基本上我想要的是包含最高赞及其相关项目名称的文档

For example the output would be例如,输出将是

"projectname":"thechosenone",
"username":"easy",
"likes":30
}
,
{
"projectname":"thanos",
"username":"wiley",
"likes":10,
}

the code i have for this is the following我的代码如下

db
    .collection("projects")
    .aggregate([
      {
        $group: {
          _id: { username: "$username" },
          likes: { $max: "$likes" }
        }
      },
      {
        $project:{projectname:1}
      }
    ])

$project gives me a strange output. $project 给了我一个奇怪的输出。 However, the output was correct without the $project.但是,没有 $project 的输出是正确的。 But i wanted to project the projectname, the user and the highest likes.但我想投射项目名称、用户和最高赞。 Thanks for hearing me out :)谢谢你听我说:)

heres the solution =)继承人的解决方案=)

db
    .collection("projects")
    .aggregate([
      {
        $sort: {
          likes: -1
        }
      },
      {
        $group: {
          _id: {
            username: "$username"
          },
          likes: {
            $max: "$likes"
          },
          projectname: {
            $push: "$projectname"
          },
          link: {
            $push: "$link"
          }
        }
      },
      {
        $project: {
          username: "$_id.username",
          projectname: {
            $arrayElemAt: ["$projectname", 0]
          },
          link: {
            $arrayElemAt: ["$link", 0]
          }
        }
      }
    ])
    .toArray()

If you don't have to use $group this will solve your problem:如果您不必使用 $group 这将解决您的问题:

db.projects.aggregate([
  {$sort:{likes:-1}},
  {$limit:1} 
]).pretty()

the result would be结果是

{
  "_id" : ObjectId("5ddee7f63cee7cdf247059db"),
  "projectname" : "thechosenone",
  "username" : "easy",
  "likes" : 30,
  "links" : ["ssss"]
}

Try this:-尝试这个:-

db.collection("projects").aggregate([
  {
    $group: {
      _id: { username: "$username" },
      likes: { $max: "$likes" },
      projectname: { $push : { $cond: [  { $max: "$likes" }, "$projectname", "" ]}}
    }
  }
  ,
  {
    $project:{
       username:"$_id.username",
       projectname:{"$reduce": {
            "input": "$projectname",
            "initialValue": { "$arrayElemAt": ["$projectname", 0] },
            "in": { "$cond": [{ "$ne": ["$$this", ""] }, "$$this", "$$value"] }
        }},
       likes:1
    }
  }
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM