2015-11-26 54 views
0

这里的课程集合:如何限制在MongoDB中返回嵌套数组场

{ 
    "_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 S):

{ 
    "_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.这适用于客户端(镀铬控制台),但它不适用于“流星芒戈”:

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

我得到的 “流星蒙戈” 错误:

“$ ERR”:“无法规范化查询:不支持的badValue投影选项:字段:{$四季情节$ paid_url:。0.0} “`

  • 尝试这样做太:

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

    不能使用'$'你需要指定你正在寻找它的确切数量不是支持minimongo –

    +0

    的查询工作,而不在镀铬$迹象控制台,但它不适用于Meteor。 – l3est

    回答

    1

    这是正确的查询:

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

    无需领域的

    +0

    在流星中仍然不起作用。结果就像'db.courses.find({_ id:“RtPA6Cxs3fzJcGpgP”})' – l3est

    +0

    @ l3est我测试了查询并且它按预期工作。 –

    +0

    @Matthias Eckhart我尝试了第一个没有获取()的查询并且它正在工作!我正在使用Meteor 1.2。 'db.courses.find({_ id:“RtPA6Cxs3fzJcGpgP”},{fields:{“Seasons.Episodes.paid_url”:0}})' – l3est