1

My below code is producing an error. If the groupBy is removed it works fine. But I only need to get distinct values for common_id. How can i solve this issue?

MasterAffiliateProductMappingMongo::select('_id', 'our_product_id')
                                   ->where('top_deal', '=', 'true')
                                   ->orderBy('srp', 'asc')
                                   ->groupBy('common_id')
                                   ->get();

Error: [MongoDB\Driver\Exception\RuntimeException] Unrecognized expression '$last'

MongoDocument Example:

{
    "_id" : ObjectId("5911af8209ed4456d069b1d1"),
    "product_id" : "MOBDRYWXFKNPZVG6",
    "our_product_id" : "5948e0dca6bc725adb35af2e",
    "mrp" : 0.0,
    "srp" : 500.0,
    "ID" : "5911af8209ed4456d069b1d1",
    "common_id" : ObjectId("5911af8209ed4456d069b1d1"),
    "top_deal" : "true"
}

Error Log:

[2017-06-28 12:19:46] lumen.ERROR: exception 'MongoDB\Driver\Exception\RuntimeException' with message 'Unrecognized expression '$last'' in /var/www/html/PaymetryService4/vendor/mongodb/mongodb/src/Operation/Aggregate.php:219

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Looks like you should turn on some logging and see what is going on . See https://stackoverflow.com/questions/41140975/laravel-eloquent-display-query-log and include the executed statement in your question if you do not understand it. – Neil Lunn Jun 28 '17 at 12:14
  • Dude. There will be an actual serialized version of the aggregation pipeline query which is being sent to the driver. That is what you are looking for. You might also include which version of MongoDB you are connecting to in your question. I'm actually half expecting you to say it's CosmosDB though. – Neil Lunn Jun 28 '17 at 12:25

1 Answers1

0

Refer to this link https://github.com/jenssegers/laravel-mongodb/issues/1185#issuecomment-321267144 it works. We should remove '_id' from the select field.

Priyadarshni
  • 184
  • 1
  • 13