代码之家  ›  专栏  ›  技术社区  ›  Morton

mongodb中如何过滤多元素?

  •  0
  • Morton  · 技术社区  · 6 年前

    以下是我的数据结构:

    {
        "_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: {
                    $filter: {
                      input: "$$movie.enName",
                      as: "movie",
                      cond: { $eq: ["$$movie", "Johnny English Strikes Again"] }
                    }
                  },
    

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

    $eq 我最初的releasedTime查询中的条件?

    1 回复  |  直到 6 年前
        1
  •  2
  •   mickl    6 年前

    你应该修改 cond 外部零件 $filter 对应用过滤条件 $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" ] }
              }
            }
        }
      }
     ])