$setWindowFields and $rank (None) must be specified with '{}' as the value

Hi,

I’ve just upgraded to MongoDB 5.0 and have to say I’m loving the new features. $rank is making things a lot cleaner for my aggregations.

I receiving the following error when running $setWindowFields and $rank in PHP

(None) must be specified with ‘{}’ as the value

The following code runs in MongoDB shell

{ 
        "$setWindowFields" : { 
            "partitionBy" : { 
                "split" : "$split_id"
            }, 
            "sortBy" : { 
                "time" : 1.0
            }, 
            "output" : { 
                "position" : { 
                    "$rank" : { 

                    }
                }, 
                "leader" : { 
                    "$first" : "$time"
                }
            }
        }
    }

The below in PHP produces the above error.

[
    '$setWindowFields' => [
        'partitionBy' => [
            'split' => '$split_id'
        ],
        'sortBy' => [
            'time' => 1.0
        ],
        'output' => [
            'position' => [
                '$rank' => []
            ],
            'leader' => [
                '$first' => '$time'
            ]
        ]
    ]
],

Any thoughts on how I should be formatting ‘$rank’ => [] in PHP?

That sure looks like a bug … file an issue ?

In the meantime, just to keep your hands busy, you might try array() instead of [] :man_shrugging:

Yes - let me have a go at this. Have attempted a few things, but not a simple array()

Solution:

[
'$setWindowFields' => [
    'partitionBy' => [
        'split' => '$split_id'
    ],
    'sortBy' => [
        'time' => 1.0
    ],
    'output' => [
        'position' => [
            '$rank' => (object) []
        ],
        'leader' => [
            '$first' => '$time'
        ]
    ]
]

],

Excellent. Just one more trap of PHP silliness :slight_smile: Thanks for the tip!
You might file an issue anyway, that’s certainly unobvious.

I did some research and apparently it’s a quirk of the MongoDB PHP Driver or Library that an empty array can’t be passed where an associative array is expected. Probably an interaction of how PHP implements non-associative arrays, thus necessitating the (object) cast. @Stennie , any comments for the curious?

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.