如何过滤mongodb中的多个元素?

How to filter multiple element in mongodb?

这是我的数据结构:

{
    "_id" : ObjectId("5bac340fe9427e6d5e769d83"),
    "theater" : "ShowTimesTaitung",
    "phone" : "089-320-388",
    "geometry" : {
        "type" : "Point",
        "coordinates" : [ 
            121.148208, 
            22.75194
        ]
    },
    "movie" : [ 
        {
            "movieStills" : [ 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/September2018/PCEHTtIh5NdUtPC6agWr-1000x667.jpg", 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/September2018/CZqSvjYsQU2JbWDVxt5O-1000x666.jpg", 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/September2018/XBlpsb4xfvKs3aRSCvJj-1000x667.jpg", 
                "https://movies.yahoo.com.tw/x/r/h340/i/o/production/movie-photos/September2018/b3VeqXiLz0dWg7fRKSWf-533x800.jpg", 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/September2018/c89ro51Ryyf4dFm5TvEG-1000x667.jpg", 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/September2018/Twb4mxWGFh8GAl87RhTl-1000x667.jpg", 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/September2018/5pnonBPrlrVkBrLLTNDV-3600x2400.jpg", 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/September2018/us5RLfsqDcwByT6Rftwv-3600x2400.jpg", 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/September2018/AgODFIHGL2VbbfCJZWRi-3600x2400.jpg", 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/September2018/nrAWnbHkzGgus8DQGhjN-3600x2400.jpg", 
                "https://movies.yahoo.com.tw/x/r/w340/i/o/production/movie-photos/September2018/13XUemflxzhcXbpZjwto-3600x2400.jpg"
            ],
            "videoId" : [ 
                "8B8U4fzwSSc", 
                "pdBm97x8D1A"
            ],
            "imdbScore" : "6.9",
            "cnName" : "凸搥特派員:三度出擊",
            "versionType" : "數位",
            "movieStyle" : [ 
                "action", 
                "joyful"
            ],
            "rottenScore" : "",
            "releasedTime" : [ 
                ISODate("2018-09-27T11:40:00.000Z"), 
                ISODate("2018-09-27T16:10:00.000Z"), 
                ISODate("2018-09-27T20:40:00.000Z"), 
                ISODate("2018-09-27T22:30:00.000Z")
            ],
            "movieTime" : "片  長:01時29分",
            "movieActorPhoto" : [ 
                "https://movies.yahoo.com.tw/x/r/h290/i/o/production/names/August2018/uWNxWH6GIgCESPqLBb9P-665x1000.jpg", 
                "https://movies.yahoo.com.tw/x/r/h290/i/o/production/names/June2017/yuY4z0JRZupbKMqiw3p3-2910x4260.jpg", 
                "https://movies.yahoo.com.tw/x/r/h290/i/o/production/names/June2017/5VYDzfwh4D9ZKaM6pFNj-2387x3000.jpg"
            ],
            "movieActorCn" : [ 
                "歐嘉柯瑞蘭蔻", 
                "艾瑪湯普遜", 
                "班奈特米勒"
            ],
            "movieDate" : "2018-09-21",
            "enName" : "Johnny English Strikes Again",
            "movieContent" : "tesst content",
            "photoHref" : "https://movies.yahoo.com.tw/x/r/w420/i/o/production/movies/September2018/SWBsUnTrn6DpDBD4UHX4-3072x4431.jpg"
        }, 
    ],
    "theaterPhoto" : "https://www.showtimes.com.tw//img/bg_logo.png",
    "address" : "address",
    "theaterCn" : "test theater name"
}

我使用此代码查询我的 releasedTime,它工作正常:

db.getCollection('Taitung').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", new Date("2018-09-27T18:40:00.000Z") ]},
                                    {$lte: [ "$$movie", new Date("2018-09-27T22:40:00.000Z") ]} 
                                  ]}
                                }
                              }
                          }
                        }
                      },
                      as: "movie",
                      cond: "$$movie"
                    }
                  }
              }
            }
        ])

我想为 enName 添加条件,所以我对 enName 进行了更改:

              enName: {
                $filter: {
                  input: "$$movie.enName",
                  as: "movie",
                  cond: { $eq: ["$$movie", "Johnny English Strikes Again"] }
                }
              },

但是显示错误信息"errmsg" : "input to $filter must be an array not string",

如何将 cnName $eq 条件添加到我原来的 releasedTime 查询中?

提前致谢。

您应该修改外部 $filtercond 部分以在 $movie 数组上应用过滤条件:

db.Taitung.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", new Date("2018-09-27T18:40:00.000Z") ]},
                          {$lte: [ "$$movie", new Date("2018-09-27T22:40:00.000Z") ]} 
                        ]}
                      }
                    }
                }
              }
            },
            as: "movie",
            cond: { $eq: [ "$$movie.enName", "Johnny English Strikes Again" ] }
          }
        }
    }
  }
 ])