Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
786 views
in Technique[技术] by (71.8m points)

node.js - Dynamic keys after $group by

I have following collection

{
    "_id" : ObjectId("5b18d14cbc83fd271b6a157c"),
    "status" : "pending",
    "description" : "You have to complete the challenge...",
}
{
    "_id" : ObjectId("5b18d31a27a37696ec8b5773"),
    "status" : "completed",
    "description" : "completed...",
}
{
    "_id" : ObjectId("5b18d31a27a37696ec8b5775"),
    "status" : "pending",
    "description" : "pending...",
}
{
    "_id" : ObjectId("5b18d31a27a37696ec8b5776"),
    "status" : "inProgress",
    "description" : "inProgress...",
}

I need to group by status and get all the keys dynamically which are in status

[
  {
    "completed": [
      {
        "_id": "5b18d31a27a37696ec8b5773",
        "status": "completed",
        "description": "completed..."
      }
    ]
  },
  {
    "pending": [
      {
        "_id": "5b18d14cbc83fd271b6a157c",
        "status": "pending",
        "description": "You have to complete the challenge..."
      },
      {
        "_id": "5b18d31a27a37696ec8b5775",
        "status": "pending",
        "description": "pending..."
      }
    ]
  },
  {
    "inProgress": [
      {
        "_id": "5b18d31a27a37696ec8b5776",
        "status": "inProgress",
        "description": "inProgress..."
      }
    ]
  }
]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not that I think it's a good idea and mostly because I don't see any "aggregation" here at all is that after "grouping" to add to an array you similarly $push all that content into array by the "status" grouping key and then convert into keys of a document in a $replaceRoot with $arrayToObject:

db.collection.aggregate([
  { "$group": {
    "_id": "$status",
    "data": { "$push": "$$ROOT" }
  }},
  { "$group": {
    "_id": null,
    "data": {
      "$push": {
        "k": "$_id",
        "v": "$data"
      }
    }
  }},
  { "$replaceRoot": {
    "newRoot": { "$arrayToObject": "$data" }
  }}
])

Returns:

{
        "inProgress" : [
                {
                        "_id" : ObjectId("5b18d31a27a37696ec8b5776"),
                        "status" : "inProgress",
                        "description" : "inProgress..."
                }
        ],
        "completed" : [
                {
                        "_id" : ObjectId("5b18d31a27a37696ec8b5773"),
                        "status" : "completed",
                        "description" : "completed..."
                }
        ],
        "pending" : [
                {
                        "_id" : ObjectId("5b18d14cbc83fd271b6a157c"),
                        "status" : "pending",
                        "description" : "You have to complete the challenge..."
                },
                {
                        "_id" : ObjectId("5b18d31a27a37696ec8b5775"),
                        "status" : "pending",
                        "description" : "pending..."
                }
        ]
}

That might be okay IF you actually "aggregated" beforehand, but on any practically sized collection all that is doing is trying force the whole collection into a single document, and that's likely to break the BSON Limit of 16MB, so I just would not recommend even attempting this without "grouping" something else before this step.

Frankly, the same following code does the same thing, and without aggregation tricks and no BSON limit problem:

var obj = {};

// Using forEach as a premise for representing "any" cursor iteration form
db.collection.find().forEach(d => {
  if (!obj.hasOwnProperty(d.status))
    obj[d.status] = [];
  obj[d.status].push(d);
})

printjson(obj);

Or a bit shorter:

var obj = {};

// Using forEach as a premise for representing "any" cursor iteration form
db.collection.find().forEach(d => 
  obj[d.status] = [ 
    ...(obj.hasOwnProperty(d.status)) ? obj[d.status] : [],
    d
  ]
)

printjson(obj);

Aggregations are used for "data reduction" and anything that is simply "reshaping results" without actually reducing the data returned from the server is usually better handled in client code anyway. You're still returning all data no matter what you do, and the client processing of the cursor has considerably less overhead. And NO restrictions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...