$unwind empty array

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

I have a collection of users where each document has following structure:

{   "_id": "<id>",   "login": "xxx",   "solved": [     {       "problem": "<problemID>",       "points": 10     },     ...   ] } 

The field solved may be empty or contain arbitrary many subdocuments. My goal is to get a list of users together with the total score (sum of points) where users that haven't solved any problem yet will be assigned total score of 0. Is this possible to do this with a single query (ideally using aggregation framework)?

I was trying to use following query in aggregation framework:

{ "$group": {   "_id": "$_id",   "login": { "$first": "$login" },   "solved": { "$addToSet": { "points": 0 } } } } { "$unwind": "$solved" } { "$group": {   "_id": "$_id",   "login": { "$first": "$login" },   "solved": { "$sum": "$solved.points" } } } 

However I am getting following error:

exception: The top-level _id field is the only field currently supported for exclusion 

Thank you in advance

回答1:

Here is the solution - it assumes that the field "solved" is either absent, is equal to null or has an array of problems and scores solved. The case it does not handle is "solved" being an empty array - although that would be a simple additional adjustment you could add.

project = {$project : {         "s" : {             "$ifNull" : [                 "$solved",                 [                     {                         "points" : 0                     }                 ]             ]         },         "login" : 1     } }; unwind={$unwind:"$s"}; group= { "$group" : {         "_id" : "$_id",         "login" : {             "$first" : "$login"         },         "score" : {             "$sum" : "$s.points"         }     } } 

db.students.aggregate( [ project, unwind, group ] );



回答2:

With MongoDB 3.2 version and newer, the $unwind operator now has some options where in particular the preserveNullAndEmptyArrays option will solve this.

If this option is set to true and if the path is null, missing, or an empty array, $unwind outputs the document. If false, $unwind does not output a document if the path is null, missing, or an empty array. In your case, set it to true:

db.collection.aggregate([     { "$unwind": {             "path": "$solved",             "preserveNullAndEmptyArrays": true     } },     { "$group": {         "_id": "$_id",         "login": { "$first": "$login" },         "solved": { "$sum": "$solved.points" }     } } ]) 


文章来源: $unwind empty array
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!