ruby on rails - Mongoid Query sum of buffer_time an integer field + Time.now which is greater than scheduled_at field -


I have a model model with buffer_time (integer) and scheduled_at (datetime) fields. I want a customized query to get

  (time now + buffer_time.mineets) & gt; Scheduled_at  

The Model Administration is here

  {: _id = & gt; BSON :: Object ID ('53D50 9 1870637330e84e0000 ') ,: buffer_time = & gt; 60,: resident_name = & gt; "Jpg" ,: Scheduled_at => 2014-08-26 14:00:00 UTC ,: Status = & gt; "Open",: updated_at = & gt; 2014-07-27 14:13:44 UTC}  

I can write it with the map but it takes a lot of time.

CRUD operations support only simple comparison and operation. If you need to calculate, the aggregation structure is recommended. You should still consider simplifying your schema, such as the Shepherd_time field by including its value in the value of the scheduled one.

The following is an answer to what you want, how to use the aggregation framework to do this. Hope you like it and helps you understand how the aggregation framework can calculate the results for matching.

test / unit / administration_task_test.rb

  'test_helper' is required 'PP' class administrativect test test & lt; ActiveSupport :: TestSege Def Setup Mongoid.default_session.drop End Test 'Multiple Tag Reference' admin_task_data = {: buffer_time = & gt; 60,: resident_name = & gt; "Jpg" ,: Scheduled_at => Time.preparation ("2014-08-26 14:00:00 UTC") ,: Status = & gt; "Open",: updated_at = & gt; Time.parse ("2014-07-27 14:13:44 UTC")} AdministrationTask.create (admin_task_data) AdministrationTask.create (admin_task_data.merge (resident_name: 'xyzzy', scheduled_at: time.now)) "\" all The administration work: "PP AdministrationTax.All.to_a run_now_task = AdministrationTab.Cololution.product ({'$ project' = & gt; {_id '= & gt;' $ _id ',' buffer_time '= & gt; $ Buffer_time, '$ Resident_name', 'scheduled_at' = & gt; '$ scheduled_at', 'position' = & gt; '$ status', 'updated_at' = & gt; '$ updated_at', 'match' = & Gt; {'$ gt' = & gt; [['$ add' = & gt; [time.nation, {'$ multiply' => ['$ buffer_time', 1000]}]}, '$ Scheduled_it ']}}}, {' $ match '=> {' match '= & gt; true}}) .to_a " The sun ta puts it now to run Scask: "pp run_on_task esrest_egele (1, run_on_task. Size) assert_equal ('xyzzy', run_now_task.first ['resident_name']) and test '0' adds the version '\' Monogoid :: VERSION: # {MukeAid :: VERSION} \ nModed :: VERSION: # {Mopped :: VERSION} "End End  

Rack Test

 < Code> run option: # running test: [1/2] administrationct test test # test_gie_magode_version mangode :: version: 3.1.6 moped :: version: 1.5.2 [2/2] administrations Reshntakt Test # Test_mltvit_tag_refr work all administration: [# & lt; AdministrationTap _id: 53f266bd7f11ba0f51000001, buffer_time: 60, resident_name: "jj", scheduled_t: 2014-08-26 14:00:00 UTC, status: "open", updated: 2014-07-27 14: 13: 44 UTC & gt; ;; # And Lieutenantes _id: 53f266bd7f11ba0f51000002, buffer_time: 60, resident_name: "exwg", scheduled_at: 2014-08-18 20:49:01 UTC, status: "open", updated date: 2014-07-27 14 : 13: 44 UTC & gt;] Administration work to run now: [{"_id" => "53f266bd7f11ba0f51000002", "buffer_time" = & gt; 60, "resident_name" = & gt; "Xyzzy", "scheduled_at" = & gt; 2014-08-18 20:49:01 UTC, "Status" = & gt; "Open", "updated_at" => 2014-07-27 14:13:44 UTC, "match = => true}] test ready in 0.524457, 3.8135 test / s, claim of 3.8135 / S2 test, 2 accusers, 0 failures, 0 errors, 0 Leave  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -