分组独特的数字monngodb
我从查找中有这些示例数据,并且
{
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需通过
ID
和no_soal
首先将其分组,然后正常删除任何等效物,然后按id
正常分组。您的示例数据和查询包含一些格式错误,但是:nofollow noreferrer“> mongoplay
Just group by both
id
andno_soal
first to remove any equivalents then group byid
normally. Your example data and query contains some formatting errors, but nevertheless:mongoplayground