parse-community / parse-php-sdk

The PHP SDK for Parse Platform
https://parseplatform.org/
Other
811 stars 346 forks source link

Aggregate lookup on array of pointers is returning empty array for lookups #433

Closed khalilsarabey closed 5 years ago

khalilsarabey commented 5 years ago

Issue Description

Hello,

I'm trying to create a function that returns the top most popular tags used by users, but I keep getting an empty array for the lookup. The way the data is saved is like the following: I have a a Tag class and a _User class. inside the _User class, I have a Tags field of type array, and it contains an array of pointers for each user, the pointers of course point to the "Tag" class.

$query = new ParseQuery('_User');
$pipeline = [
    'project' => ['tags' => 1],
    'unwind' => '$tags',
    'group' => [
        'objectId' => '$tags.objectId',
        'count' => ['$sum' => 1]
    ],
    'lookup' => [
         'from' => 'Tag',
         'localField' => 'objectId',
              'foreignField' => '_id',
               'as' => 'tags'
     ],
    'sort' => [ 'count' => -1],
    'limit' => 10,
];
try {
    return $query->aggregate($pipeline);
} catch (ParseException $ex) {
    return $ex->getMessage();
}

I tried multiple things but I keep getting the same results, not sure if it's a bug in the PHP SDK or I'm doing something wrong.

As a reference, here's a snippet of both Schema classes:

User class:

{ 
    "_id" : "5BuBVo2GD0", 
    "email" : "test@test.com", 
    "username" : "test@test.com", 
    "lastname" : "Doe", 
    "firstname" : "John", 
    "_created_at" : ISODate("2017-01-23T09:20:11.483+0000"), 
    "_updated_at" : ISODate("2019-02-15T02:48:30.684+0000"), 
    "tags" : [
        {
            "__type" : "Pointer", 
            "className" : "Tag", 
            "objectId" : "St2gzaFnTr"
        }, 
        {
            "__type" : "Pointer", 
            "className" : "Tag", 
            "objectId" : "LSVxAy2o74"
        }
    ], 
    "_p_country" : "Country$4SE8J4HRBi", 
}

Tag class:

{ "_id" : "St2gzaFnTr", "name" : "Music", "_created_at" : ISODate("2018-10-22T20:00:10.481+0000"), "_updated_at" : ISODate("2018-10-22T20:00:10.481+0000") }

I'm running Parse version 2.8.2 cause NodeChef didn't upgrade Parse that's running on their server. PHP version is: 7.0 The PHP SDK version is: 1.5.1 And the MongoDB version is: 3.4.4

Thanks

khalilsarabey commented 5 years ago

I was able to make this work. For anyone who faced an issue here's the pipeline:

        $pipeline = [
            'project' => ['tags' => 1],
            'unwind' => '$tags',
            'lookup' => [
                'from' => 'Tag',
                'localField' => 'tags.objectId',
                'foreignField' => '_id',
                'as' => 'popular'
            ],
            'group' => [
                'objectId' => '$popular',
                'count' => ['$sum' => 1]
            ],
            'sort' => [ 'count' => -1],
            'limit' => 10,
        ];

Would love to know if there's any way we can create stages with the same name, for instance I would like to use 'project' twice in the above pipeline

dplewis commented 5 years ago

can create stages with the same name

This isn't supported in the SDK. We could use a 2D array instead of an associate array for it.

I can do a PR for it.

khalilsarabey commented 5 years ago

can create stages with the same name

This isn't supported in the SDK. We could use a 2D array instead of an associate array for it.

I can do a PR for it.

I think that would be great man, sometimes stages with the same name are important, for instance what I have is good, but it would be great if I clean it up with another project stage.