Skip to content

Aggregation $lookup problem #4819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
joshkopecek opened this issue Jun 8, 2018 · 7 comments
Closed

Aggregation $lookup problem #4819

joshkopecek opened this issue Jun 8, 2018 · 7 comments

Comments

@joshkopecek
Copy link

Here we go again with my aggregation issues…

Issue Description

I want to do an aggregation query with a $lookup against a separate table, joining by _id/objectId, but because of the way Parse stores the relations (_User$0034nkjdfn) I'm not able to do an equality match between localField and foreignField.

Steps to reproduce

Collection MyPlant has a field user (_p_user) which is a pointer to the _User table
When I do an aggregate with the following pipeline:

myPlantQuery.aggregate([
        {
          lookup: {
            from: '_User',
            localField: '_p_user',
            foreignField: '_id',
            as: 'users'
          }
        }
], { useMasterKey: true })

it's trying to match the _p_user: "_User$20gruknw98g4n" with _id: "20gruknw98g4n" which of course is not going to match.

Is there any way around this?

Expected Results

Return a set of users in the 'users' field in the next pipeline stage.

Actual Outcome

Returns no users in the next pipeline stage.

Environment Setup

  • Server

    • parse-server version: 2.8.1
    • Operating System: MacOS
    • Hardware: MacBook Pro 13 Inch Mid 2012
    • Localhost or remote server? (AWS, Heroku, Azure, Digital Ocean, etc): localhost
  • Database

    • MongoDB version: 3.6
    • Storage engine: WiredTiger
    • Hardware: Same as above
    • Localhost or remote server? (AWS, mLab, ObjectRocket, Digital Ocean, etc): localhost

Logs/Trace

n/a

@joshkopecek joshkopecek changed the title $lookup with aggregation problem Aggregation $lookup problem Jun 8, 2018
@joshkopecek
Copy link
Author

I also discovered a second error with aggregation:
If you specify an object for match

{
   match: {
      user: {
        $in: myPlantUsers.map(myPlantUser => myPlantUser.id)
      }
   }
},

Parse ends up making "_User$[object Object]" as the pipeline for $match

@dplewis
Copy link
Member

dplewis commented Jun 8, 2018

check this out #1908 (comment)

hope it helps

@dplewis
Copy link
Member

dplewis commented Jun 8, 2018

@flovilmart on a side note. This is kinda related to my comment #4743 (comment)

Handling all paths will be a lot of work (recursive I dislike you). For instance $lookup will be up to the user.

#4743 needs some clean up but it does handle the 3 main paths which is a high priority. Can you look at it?

@oallouch
Copy link
Contributor

oallouch commented Jun 8, 2018

You can also check my example at #4682

@joshkopecek
Copy link
Author

@dplewis @oallouch amazing, thanks so much for this!

@joshkopecek
Copy link
Author

joshkopecek commented Jun 11, 2018

@dplewis @oallouch I can confirm this is working. Got a reasonable aggregation pipeline here:

const pipeline = [
  {
    $project: {
      userPointer: { $substr: ["$_p_user", 6, -1] },
      _p_plant: 1
    }
  },
  {
    $lookup: {
      from: "_User",
      localField: "userPointer",
      foreignField: "_id",
      as: "tempUserPointer"
    }
  },
  { $unwind: "$tempUserPointer" },
  {
    $match: {
      "tempUserPointer._p_currentRetailersGroup": /irtuvhei7dn$/
    }
  },
  {
    $group: {
      _id: "$_p_plant",
      count: { "$sum": 1 }
    }
  },
  { $sort: { count: -1 } },
  { $limit: 20 }
]

Thanks for all the help!

@oallouch
Copy link
Contributor

Just so you know, if you had a second $project after your $lookup (for instance) that wouldn't work (see #4682 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants