0

I am trying to get around the 30 second cursor time limit and avoid a MongoCursorTimeoutException by using DB->command method as seen here: MongoCursorTimeoutException for aggregate function

When I try to aggregate, command instantly returns: array(3) { ["ok"]=> float(0) ["errmsg"]=> string(42) "Invalid input resource, " ["code"]=> int(17138) } There is pretty much no documentation on this and the only think I could find is the source code where the error takes place https://github.com/mongodb/mongo/blob/master/src/mongo/db/pipeline/pipeline.cpp#L327

Can someone please help? The pipeline works fine if I run collection->aggregate(pipeline, options) so I don't think it is the pipeline. My code is below:

$connection = new MongoClient( 'mongodb://user:[email protected]' );
$summaryDB = $connection->selectDB('Summary');
$summaryCollection = $summaryDB->selectCollection('hitSummary');
//agg pipeline
        $pipeline = 
        [
            [
                '$match' => [
                    'date' => [
                        '$gte' => $weekAgo,
                        '$lt' => $today,
                    ]
                ]
            ], [
                '$group' => [
                    '_id' => [
                        'client' => '$client',
                        'date' => '$date',
                    ],
                    //conversions
                    'z1is' => [ '$sum' => '$actions.sales.import.z1.count' ],
                    'z2is' => [ '$sum' => '$actions.sales.import.z2.count' ],
                    'z3is' => [ '$sum' => '$actions.sales.import.z3.count' ],
                    'z1ps' => [ '$sum' => '$actions.sales.pixel.z1.count' ],
                    'z2ps' => [ '$sum' => '$actions.sales.pixel.z2.count' ],
                    'z3ps' => [ '$sum' => '$actions.sales.pixel.z3.count' ],
                    'z1as' => [ '$sum' => '$actions.sales.apiOnly.z1.count' ],
                    'z2as' => [ '$sum' => '$actions.sales.apiOnly.z2.count' ],
                    'z3as' => [ '$sum' => '$actions.sales.apiOnly.z3.count' ],
                    'z1ss' => [ '$sum' => '$actions.sales.s2s.z1.count' ],
                    'z2ss' => [ '$sum' => '$actions.sales.s2s.z2.count' ],
                    'z3ss' => [ '$sum' => '$actions.sales.s2s.z3.count' ],
                    //clicks
                    'z1ic' => [ '$sum' => '$actions.click.import.z1.count' ],
                    'z2ic' => [ '$sum' => '$actions.click.import.z2.count' ],
                    'z3ic' => [ '$sum' => '$actions.click.import.z3.count' ],
                    'z1pc' => [ '$sum' => '$actions.click.pixel.z1.count' ],
                    'z2pc' => [ '$sum' => '$actions.click.pixel.z2.count' ],
                    'z3pc' => [ '$sum' => '$actions.click.pixel.z3.count' ],
                    'z1ac' => [ '$sum' => '$actions.click.apiOnly.z1.count' ],
                    'z2ac' => [ '$sum' => '$actions.click.apiOnly.z2.count' ],
                    'z3ac' => [ '$sum' => '$actions.click.apiOnly.z3.count' ],
                    'z1sc' => [ '$sum' => '$actions.click.s2s.z1.count' ],
                    'z2sc' => [ '$sum' => '$actions.click.s2s.z2.count' ],
                    'z3sc' => [ '$sum' => '$actions.click.s2s.z3.count' ],
                    //impressions
                    'z1ii' => [ '$sum' => '$actions.display.import.z1.count' ],
                    'z2ii' => [ '$sum' => '$actions.display.import.z2.count' ],
                    'z3ii' => [ '$sum' => '$actions.display.import.z3.count' ],
                    'z1pi' => [ '$sum' => '$actions.display.pixel.z1.count' ],
                    'z2pi' => [ '$sum' => '$actions.display.pixel.z2.count' ],
                    'z3pi' => [ '$sum' => '$actions.display.pixel.z3.count' ],
                    'z1ai' => [ '$sum' => '$actions.display.apiOnly.z1.count' ],
                    'z2ai' => [ '$sum' => '$actions.display.apiOnly.z2.count' ],
                    'z3ai' => [ '$sum' => '$actions.display.apiOnly.z3.count' ],
                    'z1si' => [ '$sum' => '$actions.display.s2s.z1.count' ],
                    'z2si' => [ '$sum' => '$actions.display.s2s.z2.count' ],
                    'z3si' => [ '$sum' => '$actions.display.s2s.z3.count' ],
                ]
            ], [
                '$project' => [
                    'impressions' => [ '$add' => ['$z1ii', '$z2ii', '$z3ii',
                                                  '$z1pi', '$z2pi', '$z3pi',
                                                  '$z1ai', '$z2ai', '$z3ai',
                                                  '$z1si', '$z2si', '$z3si'] ],
                    'clicks' => [      '$add' => ['$z1ic', '$z2ic', '$z3ic',
                                                  '$z1pc', '$z2pc', '$z3pc',
                                                  '$z1ac', '$z2ac', '$z3ac',
                                                  '$z1sc', '$z2sc', '$z3sc'] ],
                    'importSales' => [ '$add' => ['$z1is', '$z2is', '$z3is'] ],
                     'pixelSales' => [ '$add' => ['$z1ps', '$z2ps', '$z3ps'] ],
                       'apiSales' => [ '$add' => ['$z1as', '$z2as', '$z3as'] ],
                       's2sSales' => [ '$add' => ['$z1ss', '$z2ss', '$z3ss'] ],
                ]
            ]
        ];

        //do something with this
        $options = [ 'timeout' => -1 ];

        $result = $summaryDB->command(
            [
                'aggregate' => $summaryCollection,
                'pipeline' => $pipeline,
            ],
            $options
        );


        var_dump($result);
3
  • What is $summaryCollection? It's expected to be the namespace string and not object that's the DB/collection - "hitSummary" in your case. Commented Aug 7, 2014 at 17:23
  • fixed the timeout by passing ['connectTimeoutMS' => 86400000, 'socketTimeoutMS' => 86400000] to the MongoConnect command like this: $connection = new MongoClient( 'mongodb://user:[email protected]:27018/admin', ['connectTimeoutMS' => 86400000, 'socketTimeoutMS' => 86400000] ); Commented Aug 7, 2014 at 17:25
  • see my answer - there was a simpler issue to fix. Commented Aug 7, 2014 at 17:26

1 Answer 1

1

When you run DB command, the first argument is the name of the command as the key and the value is the name of the collection.

In your case you should be passing "hitSummary" as the value of 'aggregate' and not the collection object.

See simple command example here: http://php.net/manual/en/mongodb.command.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.