如何限制在mongodb中返回的嵌套数组字段

时间:2015-11-26 13:46:27

标签: mongodb meteor underscore.js

这是课程集合:

{
  "_id": "RtPA6Cxs3fzJcGpgP",
  "Seasons": [
    {
      "title": "intro",
      "Episodes": [
        {
          "title": "what is c++?",
          "length": "12:52",
          "free_url": "free-episode-1.mp4"
        },
        {
          "title": "why c++?",
          "length": "05:20",
          "paid_url": "premium-episode-1.mp4"
        }
      ]
    },
    {
      "title": "first season",
      "Episodes": [
        {
          "title": "declare variables",
          "length": "12:35",
          "paid_url": "premium-episode-2.mp4"
        },
        {
          "title": "pointers",
          "length": "04:00",
          "free_url": "free-episode-2.mp4"
        }
      ]
    }
  ]
}

我想要(除了 paid_url 之外的所有内容):

{
  "_id": "RtPA6Cxs3fzJcGpgP",
  "Seasons": [
    {
      "title": "intro",
      "Episodes": [
        {
          "title": "what is c++?",
          "length": "12:52",
          "free_url": "free-episode-1.mp4"
        },
        {
          "title": "why c++?",
          "length": "05:20"
        }
      ]
    },
    {
      "title": "first season",
      "Episodes": [
        {
          "title": "declare variables",
          "length": "12:35"
        },
        {
          "title": "pointers",
          "length": "04:00",
          "free_url": "free-episode-2.mp4"
        }
      ]
    }
  ]
}

尝试了这些查询: 1.这适用于客户端(chrome控制台),但它不适用于“meteor mongo”:

db.courses.find({_id: "RtPA6Cxs3fzJcGpgP"}, {fields: {"Seasons.Episodes.paid_url": 0}}).fetch()

我遇到的错误是“meteor mongo”:

  

“$ err”:“无法规范化查询:BadValue不支持的投影选项:字段:{Seasons。$。Episodes。$。paid_url:0.0}”``

  1. 也尝试了这个:

    Courses.find({_id: this.params.id}, {fields: {"Seasons.$.Episodes.$.paid_url": 0}});
    

1 个答案:

答案 0 :(得分:1)

这是正确的查询:

db.courses.find({_id: "RtPA6Cxs3fzJcGpgP"}, {"Seasons.Episodes.paid_url": 0}})

不需要字段