0

Mongo collection:

item1
{
  param1: 1;
  param2: [{ status=1 }, { status=2 }, { status=4 }]
}

item2
{
  param1: 2;
  param2: [{ status=4 }, { status=2 }, { status=1 }]
}

I want to get the last status in param2 item, that value is 1. How can i generate an query?

Fedor
  • 71
  • 1
  • 1
  • 5
  • What do you have in param2 array? Is it documents with single field `status` like `{status:4}`? Also what value should be `1`? Is it value of param2? – Sergey Berezovskiy Oct 17 '13 at 13:17
  • Yes, in param2 - array. Array of objects with field 'status' and other (not only one field). Value of item2->param2->last_element->status=1 – Fedor Oct 17 '13 at 13:22

1 Answers1

0

You can use $where operator (but keep in mind that it is slow)

db.test.find({$where: function() { 
                        return this.param2.length && 
                              this.param2[this.param2.length-1].status === 1; }})
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • thanx! i can up your reputation because my reputation is low =( – Fedor Oct 17 '13 at 14:00
  • It's OK :) If it solves your problem, then you can accept it as correct answer. Also I suggest you to think about changing your collection schema design by duplicating last status in separate field as described in [this answer](http://stackoverflow.com/a/10542990/470005). – Sergey Berezovskiy Oct 17 '13 at 14:03