You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I have been searching on how to accomplish this for the last few days so this is my last ditch effort to see if this is even possible. I am trying to build the following query using the query builder:
The whereRaw aggregate does not add the field to the returned results. Also adding the additional filters after adding the new field does not work either.
I would also be ok with just running that raw query directly if that is possible? Using the WorkOrder::raw() does not work.
Any help or pointing in the right direction would be great. Thank you!
This discussion was converted from issue #2293 on July 12, 2021 20:13.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello, I have been searching on how to accomplish this for the last few days so this is my last ditch effort to see if this is even possible. I am trying to build the following query using the query builder:
db.work_orders.aggregate([ { '$match': { '$and': [ {"main_work_center": {'$in': ['WC_1', 'WC_2', 'WC_3', 'WC_4']}}, {'statuses.system': {'$in':["REL", "CRTD"]}}, {'statuses.system': {'$nin':["TECO", "CNF", "CLSD"]}}, {'scheduled_start_date': {'$lte': 20210712}}, {'scheduled_finish_date': {'$gte': 20210712}}, {'$or' : [ {'last_call.cycle_days': {'$gte': 30}}, {'maintenance_plan': null} ]} ], }, }, { '$addFields': { 'plannedWork': {'$sum': "$operations.planned_duration"} } }, { '$match': { '$and': [ { '$or' : [ {'plannedWork': {'$gte': 4}}, {'order_type': {'$in':["T1", "T3", "T6"]}} ]} ] } }, {'$sort': {'main_work_center': 1}} ])
This works great and I get the results from my database application.
I run into the issue translating to laravel:
WorkOrder::whereIn('statuses.system', ['REL', 'CRTD']) ->whereNotIn('statuses.system', ['CNF']) ->whereNotIn('statuses.system', ['TECO', 'CNF', 'CLSD']) ->whereNotIn('statuses.user', ['CNCL']) ->where('main_work_center', ['WC_1', 'WC_2', 'WC_3', 'WC_4']) ->where('scheduled_start_date', '<=', 20210712) ->where('scheduled_finish_date', '>=', 20210712) ->where(function ($q) { $q->where('last_call.cycle_days', '>=', 30) ->orWhereNull('maintenance_plan'); }) ->whereRaw(function ($collection) { return $collection->aggregate([ '$addFields' => [ 'plannedWork' => ['$sum' => '$operations.planned_duration'] ] ]); }) ->orderBy('main_work_center') ->paginate(10);
The whereRaw aggregate does not add the field to the returned results. Also adding the additional filters after adding the new field does not work either.
I would also be ok with just running that raw query directly if that is possible? Using the WorkOrder::raw() does not work.
Any help or pointing in the right direction would be great. Thank you!
Beta Was this translation helpful? Give feedback.
All reactions