【问题标题】:how to limit nested arrays field returned in mongodb如何限制在mongodb中返回的嵌套数组字段
【发布时间】:2015-11-26 13:46:27
【问题描述】:

这是课程集:

{
  "_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_urls 之外的所有内容):

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

【问题讨论】:

  • 你不能使用.$. 你需要指定你要查找的确切数字 minimongo 不支持它
  • 该查询在 Chrome 控制台中没有 $ 符号的情况下工作,但在 Meteor 中不起作用。

标签: mongodb meteor underscore.js


【解决方案1】:

这是正确的查询:

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

不需要字段

【讨论】:

  • 在流星中仍然不起作用。结果就像db.courses.find({_id: "RtPA6Cxs3fzJcGpgP"})
  • @Matthias Eckhart 我在没有 fetch() 的情况下尝试了我的第一个查询,它正在工作!我正在使用流星 1.2。 db.courses.find({_id: "RtPA6Cxs3fzJcGpgP"}, {fields: {"Seasons.Episodes.paid_url": 0}})
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多