3

I have a set of documents in a MongoDB database that describe a meeting venue. It also contains information about the opening times of a meeting venue.

Example:

{
  _id: ObjectId("54ccbb90cbcb91146d000001"),
  name: "temp_can_add_slots_to_position",
  seats: 4,
  slots: [
    {
      _id: ObjectId("54ccbb91cbcb91146d000002"),
      start_time: 1441094400,
      end_time: 1441096200,
      status: "test"
    },
    {
      _id: ObjectId("54ccbb91cbcb91146d000003"),
      start_time: 1441096200,
      end_time: 1441098000,
      status: "open"
    },
    {
      _id: ObjectId("54ccbb91cbcb91146d000004"),
      start_time: 1441098000,
      end_time: 1441099800,
      status: "open"
    },
    {
      _id: ObjectId("54ccbb91cbcb91146d000005"),
      start_time: 1441099800,
      end_time: 1441101600,
      status: "open"
    },
    {
      _id: ObjectId("54ccbb91cbcb91146d000006"),
      start_time: 1441101600,
      end_time: 1441103400,
      status: "open"
    },
    {
      _id: ObjectId("54ccbb91cbcb91146d000007"),
      start_time: 1441103400,
      end_time: 1441105200,
      status: "open"
    },
    {
      _id: ObjectId("54ccbb91cbcb91146d000008"),
      start_time: 1441105200,
      end_time: 1441107000,
      status: "open"
    },
  ],
  type: "private"
}

I want to search for the document and only return the document, with the subdocuments that have status 'open'. I can't seem to write a 'find' clause which can achieve this.

For example I'd like the document as above, unchanged except for the 'slot' subdocument with object id 54ccbb91cbcb91146d000002.

This, for example, returns the whole document:

find({_id: ObjectId("54ccbb90cbcb91146d000001"),"slots.status": "open"}).limit(10)

(Actually sits inside some Ruby which looks like this):

def list_open_slots_at_a_position(id)
    working_position = @coll.find( { "_id:" => "ObjectId(\"#{id}\")", "slots.status:" => "open" } ).to_a
    puts working_position
end

What am I doing wrong ?

Anima-t3d
  • 3,431
  • 6
  • 38
  • 56
d4vesm
  • 31
  • 2
  • 1
    Related to: http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection – Anima-t3d May 29 '15 at 20:25

0 Answers0