[SERVER-18794] Add an aggregation expression to convert an object to an array of key, value pairs Created: 03/Jun/15  Updated: 19/Jul/17  Resolved: 03/Mar/17

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

Type: New Feature Priority: Major - P3
Reporter: Lee Chi Hang Assignee: Tess Avitabile (Inactive)
Resolution: Done Votes: 15
Labels: expression, stage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
is depended on by CSHARP-1939 Add an aggregation expression to conv... Backlog
Documented
Duplicate
is duplicated by SERVER-5947 Add ability to project key names as v... Closed
is duplicated by SERVER-11392 $unwind on subdocuments Closed
Related
related to DOCS-10459 The documentation for $objectToArray ... Closed
related to DRIVERS-358 Aggregation Framework Support for 3.6 Closed
related to SERVER-27774 Add $of for iterating over object keys Closed
is related to SERVER-23310 Add an expression to convert an array... Closed
is related to SERVER-2507 variable field name in queries Backlog
Backwards Compatibility: Fully Compatible
Backport Requested:
v3.4
Sprint: Query 2017-03-06
Participants:
Case:

 Description   

In many cases, it would be useful to convert an object to an array. We should add an aggregation expression to do so. For example, it could be called $objectToArray, and do something like this:

> db.foo.insert({_id: 0, subDoc: {a: 1, b: 3, c: "example"}});
> db.foo.aggregate([{$project: {expanded: {$objectToArray: "$subDoc"}}}])
{_id: 0, expanded: [{k: "a", v: 1}, {k: "b", v: 3}, {k: "c", v: "example"}]}

Original Description

Currently, if a collection contains the following document:

{_id: 0, data: {a: 1, b: 2, c: 3}}

There is no way to unwind that one document into the following three documents:

{_id: 0, data: {a: 1}}
{_id: 0, data: {b: 2}}
{_id: 0, data: {c: 3}}

One way to accomplish this would be to add a stage to unwind documents, e.g.

db.coll.aggregate([{$objectToArray: {field: '$data'}}])

Which would yield the following document, which could then be unwound to produce the desired result:

{_id: 0, data: [{a: 1}, {b: 2}, {c: 3}]}



 Comments   
Comment by Githook User [ 28/Mar/17 ]

Author:

{u'username': u'sindbach', u'name': u'Wan Bachtiar', u'email': u'sindbach@gmail.com'}

Message: SERVER-18794 Add aggregation pipeline expression $objectToArray

Closes #1

Signed-off-by: Tess Avitabile <tess.avitabile@mongodb.com>
(cherry picked from commit 6a6bffee00e95776f7dd50e96aa0b8874ca7a01d)
Branch: v3.4
https://github.com/mongodb/mongo/commit/02eeab70014675b3c41839197c5f684ae6534f95

Comment by Tess Avitabile (Inactive) [ 08/Mar/17 ]

Okay, thank you. It is fine to leave the commit message as is. Sorry for the mistake!

Comment by Wan Bachtiar [ 08/Mar/17 ]

tess.avitabile correct. The pull-request was just on my fork https://github.com/sindbach/mongo, and not a pull-request on the https://github.com/mongodb/mongo
Please let me know if there's anything I could do to help rectify

Comment by Tess Avitabile (Inactive) [ 07/Mar/17 ]

wan.bachtiar, was there a pull request on the public repo, or just on your fork? I reviewed the one that was on your fork.

Comment by David Storch [ 03/Mar/17 ]

tess.avitabile, what was the correct pull request number? We should ensure that it is linked to this ticket for future reference.

Comment by Tess Avitabile (Inactive) [ 03/Mar/17 ]

Apologies, the pull request number in the commit message is incorrect.

Comment by Githook User [ 03/Mar/17 ]

Author:

{u'username': u'sindbach', u'name': u'Wan Bachtiar', u'email': u'sindbach@gmail.com'}

Message: SERVER-18794 Add aggregation pipeline expression $objectToArray

Closes #1

Signed-off-by: Tess Avitabile <tess.avitabile@mongodb.com>
Branch: master
https://github.com/mongodb/mongo/commit/6a6bffee00e95776f7dd50e96aa0b8874ca7a01d

Comment by Charlie Swanson [ 18/Jan/17 ]

It has come to my attention that there is an open pull request to add this feature (thanks lch1990710!). This is great! I would love if someone from the community could help us out here. Unfortunately, we cannot accept it immediately. The syntax of the new expression in the pull request does not match the syntax proposed in the top of the description.

More generally, we have not yet decided what the syntax of such an operator should be. I like the syntax of the new description since it allows manipulation of field names, and would open up several use cases (e.g. what are all the field names present in the collection?). This issue is high on the (tentative) list of expressions to add in the upcoming release, so we hope to meet up and agree on syntax in the coming months.

I will check back in when we have agreed on a specification for this addition to the language, and hopefully we can revive the pull request at that point.

Comment by John A. De Goes [ 29/Mar/16 ]

Awesome. Hope that means it gets on the roadmap, too.

Comment by Charlie Swanson [ 23/Mar/16 ]

Hi all,

After some internal discussion, we've decided that the best way forward is to add two expressions, along the lines of $objectToArray and $arrayToObject. I've converted this ticket to request the functionality of $objectToArray, and will file a separate ticket for $arrayToObject, then link that to this one.

I believe this will cover all use cases, but some cases may require use of other expressions.

Comment by Lee Chi Hang [ 06/Jun/15 ]

Ops, sorry for missing reading that haha, the idea seem similar, what od u think about which have a wider use case with other aggregate stage? Seem simply $objectToArray wilp be more user friendly.
or you want to transfer the discussion to that ticket?? I agree that better close one of the ticket.
And I will modiy and push the code after having the conclusion

Comment by Sylvain Leroux [X] [ 06/Jun/15 ]

Oh, sorry for the confusion. Did you read my comment on server-11392 ? There are slightly different approaches to this idea. Worth taking time to discuss them, I think.

Comment by Lee Chi Hang [ 06/Jun/15 ]

What I suggest in this ticket is add I function to convert object to array for example named it "$objectToArray"

you may Google it after 3.1 release

Comment by Sylvain Leroux [X] [ 06/Jun/15 ]

@Lee Chi Hang
That sounds a good idea. I wasn't even aware of `$objectToArray` – and by googling I can't even find a reference on it...

Comment by Lee Chi Hang [ 06/Jun/15 ]

I am thinking follow with $unwind is one of the use case of $objectToArray.
Other function for array can be use after $objectToArray for difference use case.
I think ticket server-11392 should close because $objectToArray and other function already cover more use cases.

Comment by Sylvain Leroux [X] [ 06/Jun/15 ]

Shouldn't be this ticket closed and consolidated with SERVER-11392 as a duplicate (like SERVER-15175 was) ? That would avoid fragmentation of the discussion.

Comment by Ramon Fernandez Marina [ 04/Jun/15 ]

lch1990710, I've closed your pull request as it contained previous commits for other tickets. Please open a new pull request with just your changes for this feature request.

Since your pull request is adding new functionality, it is strongly recommended that you add a test to check that this new functionality is working as expected. See for example the commit for the new $isArray operator as a recent example of what we expect in terms of testing for new features.

Thanks,
Ramón.

Comment by Ramon Fernandez Marina [ 03/Jun/15 ]

Thanks for you report lch1990710, we've set the fixversion to "Needs Triage" so this feature request is taken into consideration during the next planning round.

Regards,
Ramón.

Generated at Thu Feb 08 03:48:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.