$ filter

时间:2018-10-17 07:00:24

标签: mongodb aggregation-framework robo3t

我想查询我的movieStyle这是一个数组。

我认为我使用的关键词是aggregate $map $filter input as cond

我在Robo3T中使用查询命令:

db.getCollection('Tainan').aggregate([
            {                
              "$project": { 
                  theater: true,
                  theaterCn: true,
                  movie: {
                    $filter: {
                      input: {
                        $map: {
                          input: "$movie",
                          as: "movie",
                          in: {
                              cnName: "$$movie.cnName",
                              enName: "$$movie.enName",
                              versionType: "$$movie.versionType",
                              movieStyle: {
                                $filter: {
                                  input: "$$movie.movieStyle",
                                  as: "movieStyle",
                                  cond: { $eq: [ "$$movieStyle", '動作' ] }  
                                }
                              }
                          }
                        }
                      },
                    }
                  }
              }
            }
        ])

但是我得到了错误:

Error:
Assert: command failed: {
    "ok" : 0,
    "errmsg" : "Missing 'cond' parameter to $filter",
    "code" : 28650,
    "codeName" : "Location28650"
} : aggregate failed

为什么?我过滤我的movie.movieStyle会出错吗?

这是我的数据结构:

{
    "_id" : ObjectId("5bc6af1ce9427ea77e75eacc"),
    "theater" : "TodayTainan",
    "geometry" : {
        "type" : "Point",
        "coordinates" : [ 
            120.196866, 
            22.99322
        ]
    },
    "movie" : [ 
        {
            "movieStills" : [ 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/January2018/VOTtTo0hq2Sc0ncv8xYE-1000x667.jpg"
            ],
            "videoId" : [ 
                "rUAri_35CIA", 
                "gV6cD1iUp_0"
            ],
            "imdbScore" : "7.3",
            "cnName" : "蟻人與黃蜂女",
            "versionType" : "數位",
            "movieStyle" : [ 
                "\n                    動作\n                  ", 
                "\n                    科幻\n                  "
            ],
            "rottenScore" : "88%",
            "releasedTime" : [ 
                ISODate("2018-10-17T13:10:00.000Z"), 
                ISODate("2018-10-17T17:30:00.000Z"), 
                ISODate("2018-10-17T21:40:00.000Z")
            ],
            "movieTime" : "片  長:01時59分",
            "movieActorPhoto" : [ 
                "https://movies.yahoo.com.tw/x/r/h290/i/o/production/names/May2018/P7wuxMUcAILicv9huTkf-2790x2790.jpg"
            ],
            "movieActorCn" : [ 
                "保羅路德", 
                "伊凡潔琳莉莉"
            ],
            "movieDate" : "date:2018-07-04",
            "enName" : "Ant-Man and the Wasp",
            "movieContent" : "some content",
            "photoHref" : "https://movies.yahoo.com.tw/x/r/w420/i/o/production/movies/May2018/nJipiMxyde97aeNbkGkJ-1045x1500.jpg"
        }, 
        {
            "movieStills" : [ 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/May2018/Hnk8uVTwSAN50pDZt6gc-2160x900.jpeg"
            ],
            "videoId" : [ 
                "utddeo-pulY", 
                "krd8J_clTyo"
            ],
            "imdbScore" : "5.9",
            "cnName" : "摩天大樓",
            "versionType" : "數位",
            "movieStyle" : [ 
                "\n                    動作\n                  ", 
                "\n                    懸疑/驚悚\n                  "
            ],
            "rottenScore" : "46%",
            "releasedTime" : [ 
                ISODate("2018-10-17T11:10:00.000Z"), 
                ISODate("2018-10-17T15:20:00.000Z"), 
                ISODate("2018-10-17T19:40:00.000Z")
            ],
            "movieTime" : "片  長:01時43分",
            "movieActorPhoto" : [ 
                "https://movies.yahoo.com.tw/x/r/h290/i/o/production/names/June2017/o1hsVz7VPYYYEs0YuYvX-2131x3500.jpg"
            ],
            "movieActorCn" : [ 
                "巨石強森", 
                "帕布羅薛伯"
            ],
            "movieDate" : "date:2018-07-12",
            "enName" : "Skyscraper",
            "movieContent" : "some content",
            "photoHref" : "https://movies.yahoo.com.tw/x/r/w420/i/o/production/movies/May2018/sy8K5jWAfeiqv9wH25uI-947x1500.JPG"
        }
    ],
    "phone" : "06-2205151",
    "address" : "some address",
    "theaterCn" : "Today theater"
}

任何帮助将不胜感激。预先感谢。

这是我关于$filter releasedTime的代码,它可以正常工作。我只是想不通为什么我不能$filter movieStyle

db.getCollection('Tainan').aggregate([
            {                
              "$project": { 
                  theater: true,
                  theaterCn: true,
                  movie: {
                    $filter: {
                      input: {
                        $map: {
                          input: "$movie",
                          as: "movie",
                          in: {
                              cnName: "$$movie.cnName",
                              enName: "$$movie.enName",
                              versionType: "$$movie.versionType",
                              releasedTime: {
                                $filter: {
                                  input: "$$movie.releasedTime",
                                  as: "movie",
                                  cond: { $and: [
                                    {$gte: [ "$$movie", 2018-10-17 10:20:00.000Z`) ]},
                                    {$lte: [ "$$movie", 2018-10-17 22:20:00.000Z`) ]} 
                                  ]}
                                }
                              }
                          }
                        }
                      },
                      as: "movie",
                      cond: "$$movie"
                    }
                  }
              }
            }
        ])

1 个答案:

答案 0 :(得分:1)

您可以尝试以下汇总

您的movieStyle数组中的元素包含/n。您可以使用$trim聚合删除它们。

db.collection.aggregate([
  { "$project": {
    "theater": true,
    "theaterCn": true,
    "movie": {
      "$filter": {
        "input": {
          "$map": {
            "input": "$movie",
            "as": "movie",
            "in": {
              "cnName": "$$movie.cnName",
              "enName": "$$movie.enName",
              "versionType": "$$movie.versionType",
              "movieStyle": {
                "$filter": {
                  "input": "$$movie.movieStyle",
                  "as": "movieStyle",
                  "cond": {
                    "$eq": [
                      { "$trim": { "input": "$$movieStyle" }},
                      "動作"
                    ]
                  }
                }
              }
            }
          }
        },
        "as": "movie",
        "cond": "$$movie"
      }
    }
  }}
])