检索MongoDB集合中对象数组中的特定元素

时间:2016-02-22 14:08:37

标签: mongodb mongodb-query aggregation-framework

我有以下文件:

{ 
  "_id" : 257655, 
  "name" : "Arrow", 
  "airsDayOfWeek" : "Wednesday", 
  "airsTime" : "8:00 PM", 
  "firstAired" : ISODate("2012-10-10T00:00:00.000+0000"), 
  "runtime" : 45, 
  "imdbId" : "tt2193021",
  "seasons" : [
      {
          "number" : 1, 
          "episodes" : [
              {
                  "_id" : 4599381, 
                  "number" : 1, 
                  "overview" : "Diggle and Felicity  ...", 
              }, 
              {
                  "_id" : 4365918, 
                  "number" : 2, 
                  "overview" : "Oliver heads ...", 
              }
           ]
      },
      {
          "number" : 2, 
          "episodes" : [
              {
                  "_id" : 4599398, 
                  "number" : 1, 
                  "overview" : "With crime in Starling Cit  ...", 
              }, 
              {
                  "_id" : 4365989, 
                  "number" : 2, 
                  "overview" : "Oliver finds out the medicine ...", 
              }
           ]
      }
  ]
}

以下查询

db.shows.find({ 'seasons.episodes.number': 1, 'seasons.number': 1, _id: 257655 })

返回包含seasons.episodes

中所有记录的文档

我也试过这个

db.shows.aggregate([
{$match: { '$and': [ {'seasons.number': 1}, { 'seasons.episodes.number': 1}, {_id: 257655}] }},
{$project: {
    seasons: { 
      $filter: {
        input: '$seasons',
        as: 'seasons',
        cond: {$eq: ['$$seasons.episodes.number', 1]}
      }
    }
}}
])

但是我收到了一个错误:例外:无效的运营商' $ filter'

但是,我想仅使用包含seasons.episodes.number = 1和seasons.number = 1的数组来获取文档

{
seasons" : [
        {
            "number" : 1, 
            "episodes" : 
                {
                    "_id" : 4599381, 
                    "number" : 1, 
                    "overview" : "Diggle and Felicity  ...", 
                }
        }
]
}

我该怎么做?

2 个答案:

答案 0 :(得分:1)

如果您将结果限制为仅包含第一个$匹配的单个条目,则可以展开数组并根据季节和剧集重新组合它,从而将您的匹配限制为您感兴趣的系列和剧集。

<强>查询

db.shows.aggregate([
{$match: { _id: 257655 }},
{$unwind: "$seasons" },
{$unwind: "$seasons.episodes" },
{$group: { _id: { series: "$seasons.number", ep: "$seasons.episodes.number" }, seasons: { $push: "$seasons" } } },
{$match: { "_id.series": 1, "_id.ep": 1} }
]).pretty()

<强>结果

{
        "_id" : {
                "series" : 1,
                "ep" : 1
        },
        "seasons" : [
                {
                        "number" : 1,
                        "episodes" : {
                                "_id" : 4599381,
                                "number" : 1,
                                "overview" : "Diggle and Felicity  ..."
                        }
                }
        ]
}

答案 1 :(得分:0)

似乎你只想提取节目第一季(或每个节目)的第一集。如果是这种情况,在使用$ filter之后,您可能仍需要$ unwind来规范化数组,因为$ filter返回一个数组。

db.test.aggregate([
{$match: { '$and': [ {'seasons.number': 1}, { 'seasons.episodes.number': 1}, {_id: 257655}] }},
{$project: {
    season1: { 
      $filter: {
        input: '$seasons',
        as: 'season',
        cond: {$eq: ['$$season.number', 1]}
      }
    }
}},
{$unwind : '$season1'},
{$project: {
    episode1: { 
      $filter: {
        input: '$season1.episodes',
        as: 'episode',
        cond: {$eq: ['$$episode.number', 1]}
      }
    }
}}
]).pretty()

$ unwind为您提供第一季中的剧集数组,您可以再次使用$ filter来显示第一集(来自数组)。