1

I am using mongoDB library from link with CI. I want to join two tables so I found it can be done using lookup

I am using below code for it

$this->load->library('mongo_db',array('activate' => 'default'),'mongo_db');

$res = $this->mongo_db->aggregate(
    'firstTable',
    array(

        '$lookup' => array(
        'from' => 'secondTable',
        'localField' => '_id',
        'foreignField' => 'foreignKey',
        'as' => 'user',                 
    )               
));

echo '<pre>'; print_r($res);

It gives error as

Aggregation operation failed: localhost:27017: The 'cursor' option is required, except for aggregate with the explain argument

How I can add cursor in this case

I checked other similar answers but not getting how I can add cursor in this case

hrishi
  • 1,610
  • 6
  • 26
  • 43

2 Answers2

2

You can use the another codeigniter library from here. This library allows you to pass the aggregation options.

You can try below aggregation query. Set batchsize to 0 to have mongodb use server's default batch size.

$res = $this->mongo_db->aggregate(
    'firstTable',
     array(
       '$lookup' => array(
         'from' => 'secondTable',
         'localField' => '_id',
         'foreignField' => 'foreignKey',
        'as' => 'user',                 
       )                
     ),
     array('cursor' => array('batchSize' => 0))
);

More information here

s7vr
  • 73,656
  • 11
  • 106
  • 127
  • did you get a chance to try my answer ? Unfortunately the library you use don't have the argument to send option which is required from 3.4 version. – s7vr Sep 17 '18 at 18:04
  • Not getting drivers used in library "use MongoDB\Driver\{Query, Manager, Command, BulkWrite, Cursor, WriteConcern, WriteResult, ReadPreference, ReadConcern};" – hrishi Sep 18 '18 at 06:15
0

https://docs.mongodb.com/manual/reference/command/aggregate/

Specify a document that contains options that control the creation of the cursor object.

Changed in version 3.6: MongoDB 3.6 removes the use of aggregate command without the cursor option unless the command includes the explain option. Unless you include the explain option, you must specify the cursor option.

To indicate a cursor with the default batch size, specify cursor: {}.

To indicate a cursor with a non-default batch size, use cursor: { batchSize: }.

Daniel Heinrich
  • 790
  • 4
  • 12