how to match the last value of array in mongo db?

25 Views Asked by At

I'm trying to fetch some data from db. Here is my query which I have tried. My issue is that my out has dc_id which is an array. I'm using $eleMatch, which returns 1st matching element. what if I want to match last value?

tasks = $production_task_service.where({:pid=>5, :out=>{"$elemMatch"=>{:dc_id=>1}}})

my record structure looks like:

{"bid"=>nil,

 "out"=>
  [{"ts"=>2014-04-25 07:31:16 UTC,
    "uid"=>1,
    "dc_id"=>2,
    "a"=>
     {"expected"=>{"FALSE"=>"1", "TRUE"=>"0"},
      "notes"=>"asdas",
      "isit"=>"asdas",
      "datesit"=>""},
    "step"=>13,
    "g"=>1,
    "duration"=>54.467903445,
    "rejected"=>true,
    "rej_at"=>1409550263.9896142,
    "sample"=>3,
    "rej_by"=>3},
   {"ts"=>2014-09-01 09:51:22 UTC,
    "uid"=>1,
    "dc_id"=>1,
    "a"=>
     {"expected"=>{"FALSE"=>"0", "TRUE"=>"1"},
      "notes"=>"121212",
      "isit"=>"dsf",
      "datesit"=>"Sat Apr 14 1212 00:00:00 GMT+0530 (IST)"},
    "step"=>13,
    "duration"=>10.105335834,
    "rejected"=>true,
    "rej_at"=>1409566392.574016,
    "sample"=>4,
    "rej_by"=>1},
   {"ts"=>2014-09-03 10:11:40 UTC,
    "uid"=>5,
    "dc_id"=>2,
    "a"=>
     {"expected"=>{"FALSE"=>"0", "TRUE"=>"1"},
      "notes"=>"kranthi",
      "isit"=>"kranthi",
      "datesit"=>"Tue May 26 1987 00:00:00 GMT+0530 (IST)"},
    "step"=>13,
    "duration"=>20.881858257}],
 "pid"=>5,
 "rand"=>0.09438103628922767,
 "res"=>[],
 "sample"=>
  [{"id"=>3}, {"id"=>4, "for_review"=>true, "rejected"=>true, "rej_by"=>1}],
 "state"=>"completed",
 "id"=>"52c430556771518721000036"}
0

There are 0 best solutions below