Skip to content
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

invalid operator '$last' when grouping #1185

Closed
george-viaud opened this issue Apr 24, 2017 · 2 comments
Closed

invalid operator '$last' when grouping #1185

george-viaud opened this issue Apr 24, 2017 · 2 comments
Labels

Comments

@george-viaud
Copy link

Trying to do the following operation:

$query = new Lead;
$query->select('_id','first_name','last_name', 'email', 'created_at')
		->groupby('email')
		->where('created_at', '>=', $startDate)
		->where('created_at', '<', $endDate);

and then chunking because results are huge:

$chunkSize = 5000;
	
$query->chunk($chunkSize, function($leads) use(&$postFields)
	{
		Log::info("Processing chunk...");
		foreach($leads as $iLead => $lead)
		{
			// ... Do stuff with lead here ...
		}
	}
Log::info("Query Complete!");

Getting the following error

RuntimeException in Aggregate.php line 168: invalid operator '$last'
 in Aggregate.php line 168
at Server->executeCommand('mission_control', object(Command), object(ReadPreference)) in Aggregate.php line 168
at Aggregate->execute(object(Server)) in Collection.php line 191
at Collection->aggregate(array(array('$match' => array('$and' => array(array('created_at' => array('$gte' => object(UTCDateTime))), array('created_at' => array('$lt' => object(UTCDateTime)))))), array('$group' => array('_id' => array('$last' => '$_id'), 'email' => array('$last' => '$email'), 'first_name' => array('$last' => '$first_name'), 'last_name' => array('$last' => '$last_name'), 'created_at' => array('$last' => '$created_at'))), array('$limit' => '5000'), array('$project' => array('_id' => '1', 'first_name' => '1', 'last_name' => '1', 'email' => '1', 'created_at' => '1'))), array('typeMap' => array('root' => 'array', 'document' => 'array')))
at call_user_func_array(array(object(Collection), 'aggregate'), array(array(array('$match' => array('$and' => array(array('created_at' => array('$gte' => object(UTCDateTime))), array('created_at' => array('$lt' => object(UTCDateTime)))))), array('$group' => array('_id' => array('$last' => '$_id'), 'email' => array('$last' => '$email'), 'first_name' => array('$last' => '$first_name'), 'last_name' => array('$last' => '$last_name'), 'created_at' => array('$last' => '$created_at'))), array('$limit' => '5000'), array('$project' => array('_id' => '1', 'first_name' => '1', 'last_name' => '1', 'email' => '1', 'created_at' => '1'))), array('typeMap' => array('root' => 'array', 'document' => 'array')))) in Collection.php line 42
at Collection->__call('aggregate', array(array(array('$match' => array('$and' => array(array('created_at' => array('$gte' => object(UTCDateTime))), array('created_at' => array('$lt' => object(UTCDateTime)))))), array('$group' => array('_id' => array('$last' => '$_id'), 'email' => array('$last' => '$email'), 'first_name' => array('$last' => '$first_name'), 'last_name' => array('$last' => '$last_name'), 'created_at' => array('$last' => '$created_at'))), array('$limit' => '5000'), array('$project' => array('_id' => '1', 'first_name' => '1', 'last_name' => '1', 'email' => '1', 'created_at' => '1'))), array('typeMap' => array('root' => 'array', 'document' => 'array')))) in Builder.php line 307
at Collection->aggregate(array(array('$match' => array('$and' => array(array('created_at' => array('$gte' => object(UTCDateTime))), array('created_at' => array('$lt' => object(UTCDateTime)))))), array('$group' => array('_id' => array('$last' => '$_id'), 'email' => array('$last' => '$email'), 'first_name' => array('$last' => '$first_name'), 'last_name' => array('$last' => '$last_name'), 'created_at' => array('$last' => '$created_at'))), array('$limit' => '5000'), array('$project' => array('_id' => '1', 'first_name' => '1', 'last_name' => '1', 'email' => '1', 'created_at' => '1'))), array('typeMap' => array('root' => 'array', 'document' => 'array'))) in Builder.php line 307
@alok932
Copy link

alok932 commented Aug 9, 2017

This error occurs because you have selected _id field.
Change it to this.

$query->select('first_name','last_name', 'email', 'created_at')
		->groupby('email')
		->where('created_at', '>=', $startDate)
		->where('created_at', '<', $endDate);

@rlxsensors
Copy link

What happens when we need the _id after we group the data. I have the same problem but I need the _id field on the result.

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

No branches or pull requests

4 participants