【问题标题】:mongoose: how to select multiple input on $filter?猫鼬:如何在 $filter 上选择多个输入?
【发布时间】:2021-07-25 09:55:46
【问题描述】:

我有类似的数据

game: {
        game: [{...},{...}],
        seminar: [{...}],
        event: [{...},{...},{...}],
      }

和$过滤器

$filter: {
    input: "$game.game", <----- 
    as: "game",
    cond: {
      $and: [
        { $eq: ["$$game.game_type", game_type] },
        {
          $gte: [
            "$$game.game_info.game_time",
            start_date,
          ],
        },
      ],
    },
  },

我想对所有这三个数组进行查询搜索并返回到game 字段。但是,我不知道如何同时搜索它们。如何将gameseminarevent 中的对象放在一个地方进行$filter 搜索?

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    演示 - https://mongoplayground.net/p/f6O4a9js5fm

    使用$concatArrays

    db.collection.aggregate([
      {
        $project: {
          combinedArr: { // all the array values will be combined into 1 array
            $concatArrays: [
              "$game.game",
              "$game.seminar",
              "$game.event"
            ]
          },
          arr: {
            $concatArrays: [ // you'll get 1 array with individual objects
              [
                {
                  game: "$game.game"
                }
              ],
              [
                {
                  seminar: "$game.seminar"
                }
              ],
              [
                {
                  event: "$game.event"
                }
              ]
            ]
          }
        }
      }
    ])
    

    您可以直接使用concatArrays,如input: { $concatArrays: [...] }$filter

    【讨论】:

      猜你喜欢
      • 2017-01-08
      • 1970-01-01
      • 2020-02-16
      • 2021-08-11
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 2016-07-21
      相关资源
      最近更新 更多