MQL - 从另一个集合中获取数据

问题描述 投票:0回答:1

所以我有以下两个合集:

  • 过去地点
  • 正在播放

每个集合都有一些数据:

演示数据:

PastListenerLocation 有这样的数据:

{
  "UUID": "c19c7dd1c7a4f2ca",
  "timestamp":"2023-02-01T22:15:02.000+00:00",
  "location": {
    "coordinates": [
      00.000,
      00.953512
    ],
    "type": "Point"
  }}

NowPlaying 的数据如下:

{
  "artist": "Carrie Underwood",
  "song": "Garden",
  "type": "S",
  "timeplay":"2023-02-01T22:15:32.000+00:00",
  "history": [
    "c19c7dd1c7a4f2ca"
  ]
}

我尝试使用以下查询,该查询是使用

pastlocation.aggregate
获取的,但我没有运气在子数组中显示 NowPlaying 数据
NowPlayingInfo

[
  {
    /**
     * query: The query in MQL.
     */
    $match: {
      UUID: "c19c7dd1c7a4f2ca",
      timestamp: {
        $gte: ISODate("2023-02-01T22:15:02.000+00:00"),
      },
    },
  },
  {
    $lookup: {
      from: "NowPlaying",
      let: {
        uuid: "$UUID",
        timestamp: {
          $toDate: {
            $multiply: [
              {
                $toLong: "$timestamp",
              },
              1,
            ],
          },
        },
      },
      pipeline: [
        {
          $match: {
            history: {
              $in: ["$$uuid"],
            },

            $expr: {
              $and: [
                {
                  $gte: [
                    "$timeplay",
                    {
                      $subtract: [
                        {
                          $toLong: "$$timestamp",
                        },
                        180000,
                      ],
                    },
                  ],
                },
                {
                  $lte: [
                    "$timeplay",
                    {
                      $add: [
                        {
                          $toLong: "$$timestamp",
                        },
                        180000,
                      ],
                    },
                  ],
                },
              ],
            },
          },
        },
        {
          $project: {
            _id: 1,
            song: 1,
            artist: 1,
            type: 1,
          },
        },
      ],
      as: "NowPlayingInfo",
    },
  },
  {
    $addFields: {
      NowPlayingInfo: "$NowPlayingInfo",
    },
  },
]
mongodb aggregate lookup
1个回答
0
投票

您的

$lookup
管道部分几乎没有错误:

  1. 要使用变量,您应该在
    $expr
    运算符中使用它。
$expr: {
  $and: [
    {
      $in: [
        "$$uuid",
        "$history"
      ]
    },
    // Another condition
  ]
}
  1. 你在比较
    Timestamp
    类型和
    long
    类型。比较具有不同类型的值可能会导致意外和不正确的结果。您应该通过将
    timeplay
    值转换为
    long
    类型来比较相同类型的值。
{
  $gte: [
    {
      $toLong: "$timeplay"
    },
    {
      $subtract: [
        {
          $toLong: "$$timestamp"
        },
        180000
      ]
    }
  ]
}

完整的聚合查询应该是:

db.PastListenerLocation.aggregate([
  {
    //
    // query: The query in MQL.
    $match: {
      UUID: "c19c7dd1c7a4f2ca",
      timestamp: {
        $gte: ISODate("2023-02-01T22:15:02.000+00:00")
      }
    }
  },
  {
    $lookup: {
      from: "NowPlaying",
      let: {
        uuid: "$UUID",
        timestamp: {
          $toDate: {
            $multiply: [
              {
                $toLong: "$timestamp"
              },
              1
            ]
          }
        }
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $in: [
                    "$$uuid",
                    "$history"
                  ]
                },
                {
                  $and: [
                    {
                      $gte: [
                        {
                          $toLong: "$timeplay"
                        },
                        {
                          $subtract: [
                            {
                              $toLong: "$$timestamp"
                            },
                            180000
                          ]
                        }
                      ]
                    },
                    {
                      $lte: [
                        {
                          $toLong: "$timeplay"
                        },
                        {
                          $add: [
                            {
                              $toLong: "$$timestamp"
                            },
                            180000
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          }
        },
        {
          $project: {
            _id: 1,
            song: 1,
            artist: 1,
            type: 1
          }
        }
      ],
      as: "NowPlayingInfo"
    }
  }
])

Demo @ Mongo Playground

© www.soinside.com 2019 - 2024. All rights reserved.