猫鼬 $lookup 没有按预期响应

时间:2021-01-14 14:00:22

标签: mongodb mongoose aggregate lookup

我想从对象数组中查找文档,但它不返回所有索引。

Feed 文档

[
  {
    "uuid": "feed-001",
    "comments": [
       { createBy: 'user-001', text: 'Hello' },
       { createBy: 'user-001', text: 'Who there ?' },
       { createBy: 'user-002', text: 'Hi' },
    ],
  },
]

用户文档

[
  {
    "uuid": "user-001",
    "name": "Jack",
  },
  {
    "uuid": "user-002",
    "name": "Max",
  },
]

我试着用这个查询

FeedModel.aggregate([
  {
    $lookup: {
      from: 'users',
      localField: 'comments.createBy',
      foreignField: 'uuid',
      as: 'userInfo',
    },
  }
])

查询响应

但是 userInfo 不包含 3 个对象,IDK 为什么
[
  {
    "uuid": "feed-001",
    "comments": [
       { createBy: 'user-001', text: 'Hello' },
       { createBy: 'user-001', text: 'Who there ?' },
       { createBy: 'user-002', text: 'Hi' },
    ],
    "userInfo": [
       {
         "uuid": "user-001",
         "name": "Jack",
       },
       {
         "uuid": "user-002",
         "name": "Max",
       },
    ],
  }
]
我需要为每个评论获取 userInfo 谢谢

0 个答案:

没有答案