3

我在 MongoDB 数据库中有一组描述会议地点的文档。它还包含有关会议场所开放时间的信息。

例子:

{
  _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"
}

我想搜索文档并仅返回文档,其中包含状态为“打开”的子文档。我似乎无法编写可以实现此目的的“查找”子句。

例如,我想要上面的文档,除了对象 ID 为 54ccbb91cbcb91146d000002 的“插槽”子文档之外,其他文档保持不变。

例如,这将返回整个文档:

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

(实际上位于一些看起来像这样的 Ruby 中):

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

我究竟做错了什么 ?

4

0 回答 0