【问题标题】:Error in array on aggregation query with lookup & nest array of objects使用查找和嵌套对象数组进行聚合查询时数组出错
【发布时间】:2022-01-03 17:33:32
【问题描述】:

我有一个无法 100% 解决的查询问题

事实上,当用户在帖子中没有任何评论时。在 cmets 内部有一个“createdBy”,我需要在数组中查找该用户。如果没有 cmets,它返回一个带有空对象的数组,但它必须返回一个空数组,而不是空对象。

谁能帮帮我?非常感谢您!

这里是我的用户集合(数据)

[
  {
    _id: ObjectId("619d0f5df3f74665aff1a551"),
    name: "Test Name",
    surname: "Test Surname2",
    createdAt: ISODate("2021-11-11T17:21:58.624+01:00"),
    updatedAt: ISODate("2021-11-25T10:35:25.842+01:00"),
    posts: [
      {
        _id: ObjectId("619d0f5df3f74575aff1a551"),
        updatedAt: ISODate("2021-11-23T16:57:17.816+01:00"),
        createdAt: ISODate("2021-11-23T16:57:17.816+01:00"),
        content: "Test content....",
        comments: [
          {
            createdBy: ObjectId("618d4326f1668007b3b98404"),
            comment: "test comment...",
            _id: ObjectId("619dfaaaa88266dc91b9489c"),
          },
          {
            createdBy: ObjectId("618d4326f1668007b3b98404"),
            comment: "test comment...",
            _id: ObjectId("619dfc60a88266dc91b95741"),
          },
        ],
        date: ISODate("2021-11-23T16:57:17.820+01:00"),
      },
      {
        _id: ObjectId("619d0f5df3f74575aff1a551"),
        updatedAt: ISODate("2021-11-23T16:57:17.816+01:00"),
        createdAt: ISODate("2021-11-23T16:57:17.816+01:00"),
        content: "Test content....",
        comments: [],
        date: ISODate("2021-11-23T16:57:17.820+01:00"),
      },
    ],
  },
  {
    _id: ObjectId("619d0f5df3f74665aff1a551"),
    name: "Test Name",
    surname: "test surname",
    createdAt: ISODate("2021-11-11T17:21:58.624+01:00"),
    updatedAt: ISODate("2021-11-25T10:35:25.842+01:00"),
    posts: [
      {
        _id: ObjectId("619d0f5df3f74575aff1a551"),
        updatedAt: ISODate("2021-11-23T16:57:17.816+01:00"),
        createdAt: ISODate("2021-11-23T16:57:17.816+01:00"),
        content: "Test content....",
        comments: [
          {
            createdBy: ObjectId("618d4326f1668007b3b98404"),
            comment: "test comment...",
            _id: ObjectId("619dfaaaa88266dc91b9489c"),
          },
          {
            createdBy: ObjectId("618d4326f1668007b3b98404"),
            comment: "test comment...",
            _id: ObjectId("619dfe7ba88266dc91b961b6"),
          },
        ],
        date: ISODate("2021-11-23T16:57:17.820+01:00"),
      },
      {
        _id: ObjectId("619d0f5df3f74575aff1a551"),
        updatedAt: ISODate("2021-11-23T16:57:17.816+01:00"),
        createdAt: ISODate("2021-11-23T16:57:17.816+01:00"),
        content: "Test content....",
        comments: [
          {
            createdBy: ObjectId("618d4326f1668007b3b98404"),
            comment: "test comment...",
            _id: ObjectId("619dfaaaa88266dc91b9489c"),
          },
          {
            createdBy: ObjectId("618d4326f1668007b3b98404"),
            comment: "test comment...",
            _id: ObjectId("619dfc60a88266dc91b95741"),
          },
        ],
        date: ISODate("2021-11-23T16:57:17.820+01:00"),
      },
    ],
  },
];

这里是我的聚合查询

db.users.aggregate([
    { $unwind: { path: '$posts', preserveNullAndEmptyArrays: true } },
    { $unwind: { path: '$posts.comments', preserveNullAndEmptyArrays: true } },
    {
        $lookup: {
            from: 'users',
            localField: 'posts.comments.createdBy',
            foreignField: '_id',
            as: 'posts.comments.createdBy'
        }
    },
    { $unwind: { path: '$posts.comments.createdBy', preserveNullAndEmptyArrays: true } },
    {
        $group: {
            _id: { _id: '$_id', post_id: '$posts._id' },
            name: { $first: '$name' },
            posts: { $push: '$posts' },
            comments: { $push: '$posts.comments' },
        }
    },
    {
        $group: {
            _id: '$_id._id',
            name: { $first: '$name' },
            posts: {
                $push: {
                    _id: '$_id.post_id',
                    date: { $first: '$posts.date' },
                    content: { $first: '$posts.content' },
                    comments: '$comments'
                }
            }
        }
    },
])

这是一个带有失败数组的图像:

【问题讨论】:

  • 请告诉您想要什么输出并提供用户数据?
  • 所有这些都包含在我的问题中

标签: arrays mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:

您必须从展开中删除 preserveNullAndEmptyArrays 字段才能没有空对象

【讨论】:

  • 如果我删除 preserveNullAndEmptyArrays 没有 cmets 的帖子也会消失,但我需要它们
  • ok createdBy 和用户的 _id 是 ObjectId 还是其中 1 是一个字符串?
  • 因为似乎没有找到任何评论帖子的用户
  • 嗨,马可,谢谢!这是一个 ObjectId 而不是字符串
  • np 有一个美好的一天,离开吧 :)
猜你喜欢
  • 2020-01-24
  • 2021-08-13
  • 1970-01-01
  • 2018-09-10
  • 2021-01-18
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
相关资源
最近更新 更多