Mongoose $lookup 在同一集合中不起作用

发布于 2025-01-13 09:17:54 字数 1841 浏览 0 评论 0原文

我有以下数据集,其中有两种类型的对象通过字段 lType 进行区分,可以是 baseextra。 extra 有一个对 base 位置的引用。

    [
            {
                "_id": "622b6f1c7a0aca9aa252756c",
                "country": "US",
                "county": "Florida",
                "city": "Miami",
                "lType": "base",
                "displayString": "Florida,Miami",
                "__v": 0
            },
            {
                "_id": "622b6f1d7a0aca9aa252756e",
                "landmark": "Gas station",
                "baseLocation": "622b6f1c7a0aca9aa252756c",
                "lType": "extra",
                "displayString": "Florida,Miami,Gas station",
                "__v": 0
            },
            {
                "_id": "622b6f4c5d0fe602a18826a7",
                "country": "US",
                "county": "Florida",
                "city": "Tampa",
                "lType": "base",
                "displayString": "Florida,Tampa",
                "__v": 0
            },
            {
                "_id": "622b6f4c5d0fe602a18826a9",
                "landmark": "Downtown",
                "baseLocation": "622b6f4c5d0fe602a18826a7",
                "lType": "extra",
                "displayString": "Florida,Tampa,Downtown",
                "__v": 0
            }
    ]

当我尝试进行这样的聚合查找时,

    const location = await Location.collection.aggregate([{
      $lookup: {
        from: 'location',
        localField: 'baseLocation',
        foreignField: '_id',
        as: 'locationData'
      }
    }]).toArray();

即使我的 baseLocation 字段是一个 ObjectId,我也会在 locationData 中为我的 extra 位置得到一个空数组指向base位置对象。

I have the following dataset, where I have two types of objects discriminate by the field lType that can be either base or extra. extra has a ref to a base location.

    [
            {
                "_id": "622b6f1c7a0aca9aa252756c",
                "country": "US",
                "county": "Florida",
                "city": "Miami",
                "lType": "base",
                "displayString": "Florida,Miami",
                "__v": 0
            },
            {
                "_id": "622b6f1d7a0aca9aa252756e",
                "landmark": "Gas station",
                "baseLocation": "622b6f1c7a0aca9aa252756c",
                "lType": "extra",
                "displayString": "Florida,Miami,Gas station",
                "__v": 0
            },
            {
                "_id": "622b6f4c5d0fe602a18826a7",
                "country": "US",
                "county": "Florida",
                "city": "Tampa",
                "lType": "base",
                "displayString": "Florida,Tampa",
                "__v": 0
            },
            {
                "_id": "622b6f4c5d0fe602a18826a9",
                "landmark": "Downtown",
                "baseLocation": "622b6f4c5d0fe602a18826a7",
                "lType": "extra",
                "displayString": "Florida,Tampa,Downtown",
                "__v": 0
            }
    ]

When I try to do an aggregation lookup like this

    const location = await Location.collection.aggregate([{
      $lookup: {
        from: 'location',
        localField: 'baseLocation',
        foreignField: '_id',
        as: 'locationData'
      }
    }]).toArray();

I get an empty array in locationData for my extra locations even though my baseLocation field is an ObjectId pointing to a base location object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄暮涼年 2025-01-20 09:17:54

我认为你有一个错字。不应使用 from: 'location',而应为 from: 'locations'

尝试像这样更改您的代码:

const location = await Location.collection.aggregate([{
  $lookup: {
    from: 'locations',
    localField: 'baseLocation',
    foreignField: '_id',
    as: 'locationData'
  }
}]);

工作示例

I think you have a typo. Instead of from: 'location', it should be from: 'locations'.

Try to change your code like this:

const location = await Location.collection.aggregate([{
  $lookup: {
    from: 'locations',
    localField: 'baseLocation',
    foreignField: '_id',
    as: 'locationData'
  }
}]);

Working example

泪是无色的血 2025-01-20 09:17:54

如果您需要更多“深度”链接位置,您可以使用“$graphLookup”

[在加油站找到糖果机。]

db.locations.aggregate([
  {
    "$graphLookup": {
      "from": "locations",
      "startWith": "$baseLocation",
      "connectFromField": "baseLocation",
      "connectToField": "_id",
      "as": "locationData"
    }
  }
])

mongoplayground.net 上尝试一下

In case you need more "depth" linking locations, you could use "$graphLookup".

[Finds the Candy Machine at the Gas Station.]

db.locations.aggregate([
  {
    "$graphLookup": {
      "from": "locations",
      "startWith": "$baseLocation",
      "connectFromField": "baseLocation",
      "connectToField": "_id",
      "as": "locationData"
    }
  }
])

Try it on mongoplayground.net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文