$lookup support for nested localField

269 views
Skip to first unread message

Astro

unread,
Apr 25, 2016, 3:38:52 AM4/25/16
to mongodb-user
Can we use $lookup with nested value for localField?

I have following $lookup stag:

{$lookup:{ from:"users", localField:"appinfo[0].user_ids[0]", foreignField: "_id", as:"userinfo"}}])

where appinfo is a nested array: 

appinfo=[ { name:'xyz', active: true, user_ids=['user1', 'user2', 'user3'] } ]

Any help will be appreciated.

Asya Kamsky

unread,
Apr 25, 2016, 8:48:24 PM4/25/16
to mongodb-user
Argument to localField must be a string (which is the name of the
field). So you will need to transform (via $project) the value you
need into a field and then use that field name in the $lookup.

Something like this:
[ {$project:{ appinfo_user_id: {$arrayElemAt: [ {$arrayElemAt :
["$appinfo.user_ids,0]}, 0]} }},
{$lookup:{ from:"users", localField:"appinfo_user_id", foreignField:
"_id", as:"userinfo"}} ] )


> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user"
> group.
>
> For other MongoDB technical support options, see:
> https://docs.mongodb.org/manual/support/
> ---
> 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.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/f5658b03-417b-41b6-b138-048ce499fc2a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Asya Kamsky
Lead Product Manager
MongoDB
Download MongoDB - mongodb.org/downloads
Free MongoDB Monitoring - cloud.mongodb.com
Free Online Education - university.mongodb.com
Get Involved - mongodb.org/community
We're Hiring! - https://www.mongodb.com/careers
Reply all
Reply to author
Forward
0 new messages