MongoDB -如何使用$lookup连接两个文档

b5buobof  于 2023-08-04  发布在  Go
关注(0)|答案(2)|浏览(150)

我一直在尝试使用聚合函数连接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

zvms9eto

zvms9eto1#

您的查询是关于Student集合的。所以localField应该是_idforeignField应该是_user(来自wall集合)。
那就没问题了

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

字符串
https://mongoplayground.net/p/r1JeQIlM7AA

balp4ylt

balp4ylt2#

你混淆了localFieldforeignField
从具有单个联接条件的相等匹配:

{
   $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

相关问题