【问题标题】:how to join two documents using $lookup mongodb如何使用 $lookup mongodb 连接两个文档
【发布时间】:2023-01-17 16:57:54
【问题描述】:

我一直在尝试使用聚合函数在 MongoDb 中加入两个集合,但它似乎对我不起作用,当我使用查找运行 api 时,它显示我是空集合 [],

我尝试了以下内容。

db.student.aggregate([
  {
    "$match": {
      _id: "63c4c245188267e988d690e2"
    },
    
  },
  {
    "$lookup": {
      "from": "wall",
      "localField": "_user",
      "foreignField": "_id",
      "as": "wallpost"
    }
  }
])

结果:这是我查找后得到的结果:(

[
  {
    "_id": "63c4c245188267e988d690e2",
    "hereFor": [
      {
        "mainTag": "study",
        "subtag": [
          "studybuddy",
          "findtutor"
        ]
      }
    ],
    "lastName": "Kumar",
    "name": "Kamal",
    "profilePicture": [
      "https://airustudentimages.s3.ca-central-1.amazonaws.com/1673588594404-ba7777ef676f439f86aa612e8be67fd9"
    ],
    "wallpost": []
  }
]

收藏品我在查询中使用的集合。

学生

student: [
    {
      "_id": "63c4c245188267e988d690e2",
      "name": "Kamal",
      "lastName": "Kumar",
      "profilePicture": [
        "https://airustudentimages.s3.ca-central-1.amazonaws.com/1673588594404-ba7777ef676f439f86aa612e8be67fd9"
      ],
      "hereFor": [
        {
          "mainTag": "study",
          "subtag": [
            "studybuddy",
            "findtutor"
          ]
        }
      ],
      
    },
    {
      "_id": "63c3965c201a1d738ab6867e",
      "name": "Suraksha",
      "lastName": "Singh",
      "profilePicture": [
        "https://airustudentimages.s3.ca-central-1.amazonaws.com/1673762449670-a8bdf9b9b0faf3ad84e0a5bc76e32fb8"
      ],
      "hereFor": [
        {
          "mainTag": "study",
          "subtag": [
            "studybuddy",
            "findtutor"
          ]
        }
      ],
      
    }
  ],

"wall": [
    {
      "_id": "63c4c92a188267e988d690e3",
      "_user": "63c3965c201a1d738ab6867e",
      "isSponsered": false,
      "description": "Hello test case ",
      "tag": {
        "mainTag": "hostels"
      },
      "createdAt": "1673766717308",
      "updatedAt": "1673766717308",
      
    },
    {
      "_id": "63c4cc2b188267e988d690e5",
      "_user": "63c3965c201a1d738ab6867e",
      "isSponsered": false,
      "description": "Hello test case 22 ",
      "tag": {
        "mainTag": "hostels"
      },
      "createdAt": "1673766717308",
      "updatedAt": "1673766717308",
      
    },
    
  ],

看看https://mongoplayground.net/p/2moDXi3lygL

【问题讨论】:

    标签: node.js mongodb lookup


    【解决方案1】:

    您混淆了 localFieldforeignField

    来自Equality Match with a Single Join Condition

    {
       $lookup:
         {
           from: <collection to join>,
           localField: <field from the input documents>,
           foreignField: <field from the documents of the "from" collection>,
           as: <output array field>
         }
    }
    

    它应该是:

    {
      "$lookup": {
        "from": "wall",
        "localField": "_id",
        "foreignField": "_user",
        "as": "wallpost"
      }
    }
    

    Demo @ Mongo Playgound

    【讨论】:

      【解决方案2】:

      您的查询在Student 收藏。所以localField应该是_id并且foreignField应该是_user(来自wall集合)。

      然后它工作正常。

      db.student.aggregate([
        {
          "$match": {
            _id: "63c3965c201a1d738ab6867e"
          },
          
        },
        {
          "$lookup": {
            "from": "wall",
            "localField": "_id",
            "foreignField": "_user",
            "as": "wallpost"
          }
        }
      ])
      
      

      https://mongoplayground.net/p/r1JeQIlM7AA

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-20
        • 2015-03-30
        • 1970-01-01
        • 2019-03-20
        • 2020-12-31
        • 2019-02-06
        • 2020-05-11
        相关资源
        最近更新 更多