分组独特的数字monngodb

发布于 2025-01-27 04:36:27 字数 1556 浏览 2 评论 0原文

我从查找中有这些示例数据,并且

{
  id: 1a1,
  package:2ab,
  question:{
  no_soal:1,
  score:10
  },
  id: 1a1,
  package:2ab,
  question:{
  no_soal:1,
  score:20
  },
  id: 1a1,
  package:2ab,
  question:{
  no_soal:2,
  score:20
  },
  id: 1a1,
  package:2ab,
  question:{
  no_soal:3,
  score:5
  },
  id: 1a1,
  package:2ab,
  question:{
  no_soal:4,
  score:10
  },
  id: 1a1,
  package:2ab,
  question:{
  no_soal:4,
  score:20
  },
  id: 1b2,
  package:1ab,
  question:{
  no_soal:1,
  score:10
  },
  id: 1b2,
  package:1ab,
  question:{
  no_soal:2,
  score:10
  },
}

很好地放松了我必须按ID和软件包进行分组,然后将问题作为一个阵列对象,因此将其作为一个bellow。 NO_SOAL必须包含从第一个数据中获取的唯一数字。这是预期的输出

{
  id:1a1,
  package:2ab,
  question:[
    {no_soal:1,score:10},{no_soal:2,score:20},{no_soal:3,score:5},{no_soal:4,score:10}
  ]
},
{
  id:1b2,
  package:1ab,
  question:[
    {no_soal:1,score:10},{no_soal:2,score:10}
  ]
}

现在,我能够使用此代码重组它:

{
 $group:{
  "id": "$id",
  "package": {
    "$first": "$package"
  },
  "question": {
    "$push": "$question"
  }
 }
}

但仍然与预期的输出不那么相同。问题字段仍然不包含唯一的数字,有点混淆了如何做到这一点。你们能告诉我在此代码中应该添加什么?感谢您试图回答这个问题,

这是我从我写的代码中获得的 false Output

 {
  id:1a1,
  package:2ab,
  question:[
    {no_soal:1,score:10}{no_soal:1,score:20},{no_soal:2,score:20},{no_soal:3,score:5}, 
    {no_soal:4,score:10},{no_soal:4,score:20}
  ]
},
{
  id:1b2,
  package:1ab,
  question:[
    {no_soal:1,score:10},{no_soal:2,score:10}
  ]
}

I have these example data from lookup and unwind

{
  id: 1a1,
  package:2ab,
  question:{
  no_soal:1,
  score:10
  },
  id: 1a1,
  package:2ab,
  question:{
  no_soal:1,
  score:20
  },
  id: 1a1,
  package:2ab,
  question:{
  no_soal:2,
  score:20
  },
  id: 1a1,
  package:2ab,
  question:{
  no_soal:3,
  score:5
  },
  id: 1a1,
  package:2ab,
  question:{
  no_soal:4,
  score:10
  },
  id: 1a1,
  package:2ab,
  question:{
  no_soal:4,
  score:20
  },
  id: 1b2,
  package:1ab,
  question:{
  no_soal:1,
  score:10
  },
  id: 1b2,
  package:1ab,
  question:{
  no_soal:2,
  score:10
  },
}

well i have to group it by the id and package and make the question as an array object like this one bellow. The no_soal must contain unique number taken from the first data. this is the EXPECTED OUTPUT

{
  id:1a1,
  package:2ab,
  question:[
    {no_soal:1,score:10},{no_soal:2,score:20},{no_soal:3,score:5},{no_soal:4,score:10}
  ]
},
{
  id:1b2,
  package:1ab,
  question:[
    {no_soal:1,score:10},{no_soal:2,score:10}
  ]
}

right now i alrady able to regroup it with this code:

{
 $group:{
  "id": "$id",
  "package": {
    "$first": "$package"
  },
  "question": {
    "$push": "$question"
  }
 }
}

but still not as same as the expected output. the question field still not contain unique number, kinda confuse how to make it so. Can you guys tell me what should i add in this code? thank you for trying to answer this question

this is the FALSE OUTPUT that i get from the code i wrote

 {
  id:1a1,
  package:2ab,
  question:[
    {no_soal:1,score:10}{no_soal:1,score:20},{no_soal:2,score:20},{no_soal:3,score:5}, 
    {no_soal:4,score:10},{no_soal:4,score:20}
  ]
},
{
  id:1b2,
  package:1ab,
  question:[
    {no_soal:1,score:10},{no_soal:2,score:10}
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看春风乍起 2025-02-03 04:36:27

只需通过IDno_soal首先将其分组,然后正常删除任何等效物,然后按id正常分组。您的示例数据和查询包含一些格式错误,但是:

db.collection.aggregate([
  {
    $group: {
      "_id": {
        "id": "$id",
        "no_soal": "$question.no_soal"
      },
      "package": {
        "$first": "$package"
      },
      "question": {
        "$first": "$question"
      }
    }
  },
  {
    $group: {
      "_id": "$_id.id",
      "package": {
        "$first": "$package"
      },
      "question": {
        "$addToSet": "$question"
      }
    }
  }
])

nofollow noreferrer“> mongoplay

Just group by both id and no_soal first to remove any equivalents then group by id normally. Your example data and query contains some formatting errors, but nevertheless:

db.collection.aggregate([
  {
    $group: {
      "_id": {
        "id": "$id",
        "no_soal": "$question.no_soal"
      },
      "package": {
        "$first": "$package"
      },
      "question": {
        "$first": "$question"
      }
    }
  },
  {
    $group: {
      "_id": "$_id.id",
      "package": {
        "$first": "$package"
      },
      "question": {
        "$addToSet": "$question"
      }
    }
  }
])

mongoplayground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文