【问题标题】:Get Related schema fields from Mongoose in Node从 Node 中的 Mongoose 获取相关模式字段
【发布时间】:2023-03-17 04:44:01
【问题描述】:

我已经创建了用户架构,我正在使用不同用户发布的 cmets 存储用户详细信息。

当前数据架构。

{
    _id: '1233333',
    name: 'John',
    profileImage: 'http:/test.com',
    coments: [{
            commentBy: ObjectId(UserId),
            comment: 'good '
        }, {
            commentBy: ObjectId(UserId),
            comment: 'good'
        }
    ]
}

当我获取用户数据时,我想在 cmets 数组中获取用户名、个人资料图像 URL,我没有存储 profileimageURL、名称,因为用户会随时更新用户数据。

我想要预期的数据

{
    _id: '1233333',
    name: 'John',
    profileImage: 'http:/test.com',
    coments: [{
            commentBy: ObjectId(UserId),
            comment: 'good ',
            profileImage: 'http:/test.com',
            name : 'John'
        }, {
            commentBy: ObjectId(UserId),
            comment: 'good ',
            profileImage: 'http:/test.com',
            name : 'Tim'
        }
    ]
}

请告诉我查询以一次性获取数据..请告知

【问题讨论】:

  • 有两个选项您可以使用aggregation lookup 查询或virtual 查询进行聚合查询https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/ 按照此链接和虚拟查询UserSchema.virtual('commentBy',{ ref: 'user',//this the collection name localField: 'coments.commentBy', foreignField: '_id', justOne:true }); UserSchema.set('toObject', { virtuals: true }); UserSchema.set('toJSON', { virtuals: true });

标签: node.js mongoose mongoose-schema


【解决方案1】:

您可以使用 mongo aggregation 处理这种情况,如下所示:

db.getCollection('user').aggregate([

  { $unwind: { path: '$coments' } },
  
  { $lookup: {
        from: 'user',
        localField: 'coments.commentBy',
        foreignField: '_id',
        as: 'commentBy',
      }
    }, 

  { $unwind: { path: '$commentBy' } },

  { $group: {
    _id: '$_id',
    name: { $first: '$name' } ,
    profileImage: { $first: '$profileImage' },
    coments: { $push: {
                      commentBy: "$coments.commentBy",
                      comment: "$coments.comment",
                      profileImage: "$commentBy.profileImage",
                      name: "$commentBy.name"} 
              } 
    }}])

首先,您 unwind coments 数组(您的示例中的拼写错误)将每个评论放在单独的文档中。然后您可以使用$lookup 提取在commentBy 字段中声明的用户。然后你group 文件连同所需的格式。

【讨论】:

    【解决方案2】:

    您也可以在不使用聚合的情况下实现此目的,有时聚合管道运行缓慢。 在您的用户模式中,您可以使用虚拟人口。像这样

    UserSchema.virtual('commentBy',{
        ref: 'user',//this the collection name
        localField: 'coments.commentBy',
        foreignField: '_id',
        justOne:true
    });
    
    UserSchema.set('toObject', { virtuals: true });
    UserSchema.set('toJSON', { virtuals: true });
    

    然后以这种方式查找并填充它

    UserModel.find({})
          .lean()
          .populate('commentBy')
          .then((data) => {
            if (data.length > 0) {
              result(data);
            } else {
              console.log("Data Not Exists");
            }
          });
    

    【讨论】:

      猜你喜欢
      • 2016-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      相关资源
      最近更新 更多