[SERVER-23729] Crafting subdocuments with the aggregation framework Created: 01/Apr/16  Updated: 20/Apr/16  Resolved: 15/Apr/16

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Niccolò Belli Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-23310 Add an expression to convert an array... Closed
Participants:

 Description   

Hi,
I would like to use the aggregation framework for tasks like listing the most popular tags per day, unfortunately I can't do it without some client side logic because the aggregation framework allows to craft arrays but not subdocuments (objects).
This is a full explanation of what I would like to achieve:
http://stackoverflow.com/questions/36203290/mongodb-aggregation-framework-list-of-most-popular-tags-per-day



 Comments   
Comment by Ramon Fernandez Marina [ 15/Apr/16 ]

I think the requested feature is a duplicate of SERVER-23310, in the sense that once SERVER-23310 is implemented it would be trivial to achieve the functionality described in this ticket by using the $arrayToObject operator.

darkbasic, please take a look at Asya's blog for examples on using the aggregation pipeline, I think you should be able to accomplish what you need even if your pipeline is a bit more elaborate.

Playing a bit with the example in stackoverflow I came up with this:

> db.test.aggregate([
  {$unwind : "$tags" },
 
  {$group : {
     _id : { "d" : "$day", "t" : "$tags" },
     count : {"$sum" : 1}
  }},
 
  {$group : {
     _id : "$_id.d",
     tags : {$push : { t : "$_id.t", c : "$count"}}}
  },
 
]);
 
{ "_id" : "2010/01/12", "tags" : [ { "t" : "sledding", "c" : 2 }, { "t" : "winter", "c" : 10 }, { "t" : "nosql", "c" : 4 } ] }
{ "_id" : "2010/01/13", "tags" : [ { "t" : "soda", "c" : 5 }, { "t" : "php", "c" : 2 } ] }
{ "_id" : "2010/01/14", "tags" : [ { "t" : "python", "c" : 6 }, { "t" : "nosql", "c" : 15 }, { "t" : "winter", "c" : 4 } ] }

Hope that helps.

Comment by A. Jesse Jiryu Davis [ 14/Apr/16 ]

I see. I've moved this to the Core Server project.

Comment by Niccolò Belli [ 14/Apr/16 ]

Hi, sorry if I'm not been clear. Mine is not a question: it's a feature request. Currently the aggregation framework allows to craft an array using the $push operator, the feature I'm requesting for is the ability to craft objects instead of arrays. I want the result to be a document with subdocuments. I need something like "$addToSubdocument" : {"subdocument_name" : {"$key" : "$value"}} to be able to push my tags and their values inside my "tags" subdocument. My use case is well explained in the stackoverflow question, that's whay I linked it.

Comment by A. Jesse Jiryu Davis [ 14/Apr/16 ]

Hi Niccolo, this is the ticket tracker for the MongoDB C Driver project. If you have a question about how to use the MongoDB database, Stack Overflow is the right place to get help.

Generated at Thu Feb 08 04:04:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.