unwind of mongo object

1,458 views
Skip to first unread message

Petar Georgiev

unread,
Feb 8, 2014, 4:11:31 AM2/8/14
to mongod...@googlegroups.com
Hi all

I would like to use aggregation framework to unwind not an array but  Json object ... is it possible ?

I have one more question - if i have json object like this

{
_id : 5,
attributes : {
                   a:1 ,
                   b : 2
                  }
}

is it possible with aggregation framework to get this transformation :

{
_id :a,
field: 5
}

{
_id : b
field: 5
}

i don't know how to access obect keys (a and b) with aggregation framework.

Best Regards,
Petar

Petar Georgiev

unread,
Feb 8, 2014, 4:27:37 AM2/8/14
to mongod...@googlegroups.com
Just one more question:

is it possible to covert json object keys to array by using aggregation framework:

{ _id:5,
  attr: {
    a:1,
    b:2
}
}

to get this by using aggregation framework

{_id:5,
   attr: [a,b]

Asya Kamsky

unread,
Feb 8, 2014, 9:40:01 PM2/8/14
to mongodb-user
You cannot unwind anything that's not an array.

What is it that you want to get - do you have multiple fields with
different names where you have the string "attributes"?

It's only possible to use agg framework (or querying) if you know all
the possible values of "attributes".

If you don't then you need to use a different schema to do what you
are trying to do.

If you know all the possible "attributes" string values then you can
do some aggregations. If you give a more specific example, I should
be able to point you to a possible solution.

Asya
> --
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb
>
> ---
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongodb-user...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Petar Georgiev

unread,
Feb 9, 2014, 6:04:57 PM2/9/14
to mongod...@googlegroups.com


08 февруари 2014, събота, 11:11:31 UTC+2, Petar Georgiev написа:

Petar Georgiev

unread,
Feb 9, 2014, 6:05:14 PM2/9/14
to mongod...@googlegroups.com
Hi Asya,

I'm going to give you a 'twitter' example

if an json object represent user and this object contains the id's of user which follows that users in manner:
{
userId: 1,
  followed : {
           10 : Jack,
            11 : Jon
  }
}

that means - user 1 is followed by user 10 and 11. I need to get  relation following - user x if following user y

so i need to transform the exposed json object into:

{
  {
   _id : 10,
   following: 1
  },

  {
   _id : 11,
   following: 1
  }

}

that means user 11 is following user 1 and so on ...

I know that this is map-reduce business but i want to do that transformation with aggregation framework ...

Best Regards,
Petar 

Asya Kamsky

unread,
Feb 9, 2014, 6:25:28 PM2/9/14
to mongodb-user
This seems like a very awkward schema to represent followers - keyname being their userId is a poor choice, in my opinion.

It makes it impossible to do any sort of meaningful querying or updating with that structure.

I think you're right to be looking for ways to transform this schema - mapReduce will be able to do it as you can iterate over the keys of the object.

I did file an enhancement request a while back to allow mapping key names to values and vice versa - feel free to upvote this ticket.

Asya

Reply all
Reply to author
Forward
0 new messages