如何使用多个查询函数返回一个没有重复数组的对象?

时间:2018-10-23 04:11:27

标签: javascript mongodb mongoose

我有很多城市集合,我想用一种方法... D/vol.MediaSessions: onPlaybackStateChanged com.spotify.music STATE_PLAYING PlaybackState {state=3, position=421... ... D/vol.MediaSessions: onPlaybackStateChanged com.google.android.youtube STATE_PLAYING PlaybackState ... 来查询不同的集合,该方法只能返回一个对象并且没有重复的数组数据getMovieStyle(req, ers, next) { },可能是过滤器{{1} }

我使用movie,这是我的基本查询功能,我通过enName返回数据:

mongoos

res.send(data)结构为:

getMovieStyle(req, res, next) {
    const { movieStyle } = req.query;

    const cityCollection = mongoose.model('city1', MovieSchema, 'city1');

    cityCollection.aggregate([
        { "$match": {
              "theater": "Centuryasia"
            }
        },
        {                
           "$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: {
                                $in: [
                                  {
                                    $trim: {
                                      input: "$$movieStyle"
                                    }
                                  },
                                  ['動作', '冒險']
                                ]
                              }
                            }
                          }
                      }
                    }
                  },
                  as: "movie",
                  cond: "$$movie"
                }
              }
          }
        }
    ])
        .then(data => res.send(data))
        .catch(next);

},

下一步,我将使用

res.send(data)

如果我使用{ "_id" : ObjectId("5bc6b2c1e9427ea77e75eb1b"), "theater" : "Centuryasia", "theaterCn" : "喜樂時代影城", "movie" : [ { "cnName" : "滴答屋", "enName" : "The House with a Clock in its Walls", "versionType" : "數位", "movieStyle" : [ "\n 喜劇\n " ] }, { "cnName" : "凸搥特派員:三度出擊", "enName" : "Johnny English Strikes Again", "versionType" : "數位", "movieStyle" : [ "\n 動作\n ", "\n 喜劇\n " ] }, { "cnName" : "格林納威:胡說爸道", "enName" : "The Greenaway Alphabet", "versionType" : "數位", "movieStyle" : [] } ] } ,影片数组数据将被覆盖。

如何将const cityCollection2 = mongoose.model('city2', MovieSchema, 'city2'); cityCollection2.aggregate([ ... ]).then(data2 => myData2Object = data2); const cityCollection3 = mongoose.model('city3', MovieSchema, 'city3'); cityCollection3.aggregate([ ... ]).then(data3 => myData3Object = data3); Object.assign(target, ...sources) myData1Object与一个对象和myData2Object数组组合在一起而没有重复数据(按myData3Object过滤)?

第二个问题是,如果我想查询不同的城市集合,是否还有另一种最佳方法来实现呢?

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

0 个答案:

没有答案