如何在 mongoose 中填充已填充的字段名称

问题描述 投票:0回答:1

这是我的示例填充结果:

我需要从“fileInfo”填充填充的字段名称 在 fileInfo 中我需要填充文件名“createdBy”

{
  _id: new ObjectId("6566efabb6dcd34a6d20414f"),
  documentNumber: 'DOC-PDF-4',
  documentTitle: 'PDF_4',
  fileInfo: {
    _id: new ObjectId("6566ef99b6dcd34a6d204146"),
    name: '360_F_312167890_QseKj8EJU4q4hSoCMq0suVibnjRyM6rd.jpg',
    size: 32163,
    resourceKey: '5DA4FtT_MxxA1EIz_w0dt_360_F_312167890_QseKj8EJU4q4hSoCMq0suVibnjRyM6rd.jpg',
    resourcePath: '655c377de5a501b11cbf5d99/documents',
    accountId: new ObjectId("655c377de5a501b11cbf5d99"),
    createdOn: 2023-11-29T08:00:25.542Z,
    modifiedOn: 2023-11-29T08:00:25.542Z,
    createdBy: new ObjectId("655c377de5a501b11cbf5d94"),
    modifiedBy: new ObjectId("655c377de5a501b11cbf5d94"),
    fileType: 'image/jpeg',
    __v: 0
  },
  __v: 0
}

我想填充“fileInfo”对象内的“createdBy”字段。

“createdBy”字段位于用户模型中。 这是文档模型

我已经尝试过但没有得到结果


      Document.findOne({ _id: new mongoose.Types.ObjectId(documentId) })
        .populate(['labels', 'fileInfo'])
        .then(function (documentView) {

          User.populate(documentView.fileInfo,
            { path : 'fileInfo.createdBy',
              select: 'email',
              model: 'User'});

          console.log(documentView, 'documentView');
          return next(null, documentView);
        })

这就是我所期待的。


{
  _id: new ObjectId("6566efabb6dcd34a6d20414f"),
  documentNumber: 'DOC-PDF-4',
  documentTitle: 'PDF_4',
  fileInfo: {
    _id: new ObjectId("6566ef99b6dcd34a6d204146"),
    name: '360_F_312167890_QseKj8EJU4q4hSoCMq0suVibnjRyM6rd.jpg',
    size: 32163,
    resourceKey: '5DA4FtT_MxxA1EIz_w0dt_360_F_312167890_QseKj8EJU4q4hSoCMq0suVibnjRyM6rd.jpg',
    resourcePath: '655c377de5a501b11cbf5d99/documents',
    accountId: new ObjectId("655c377de5a501b11cbf5d99"),
    createdOn: 2023-11-29T08:00:25.542Z,
    modifiedOn: 2023-11-29T08:00:25.542Z,
    createdBy: {
        "_id" : ObjectId("655c377de5a501b11cbf5d94"),
        "name" : "Rohit",
        "isSupportUser" : false,
        "isActivated" : true,
        "isSubscribed" : false,
        "activateBttnClickCount" : 0,
        "primarySupport" : false,
        "__v" : 0
    },
    modifiedBy: new ObjectId("655c377de5a501b11cbf5d94"),
    fileType: 'image/jpeg',
    __v: 0
  },
  __v: 0
}

这是下面的文档架构


const DocumentSchema = new Schema({
  documentNumber: {type:String, es_indexed:true ,  uppercase: true},
  documentTitle: String,
  folder: String,
  folderLevel: String,
  accountId: mongoose.Schema.Types.ObjectId,
  labels: [{ type: Schema.Types.ObjectId, ref: 'Label' }],
  revisionNumber: String,
  comments: String,
  uploadComments: String,
  fileInfo: { type: Schema.Types.ObjectId, ref: 'Attachment' },
  imageFileInfo: [{ type: Schema.Types.ObjectId, ref: 'Attachment' }],
  includeInSmartFile: String,
  documentDate: Date,
});

提前致谢

mongodb mongoose mongodb-query mongoose-populate
1个回答
1
投票

您似乎正在使用 Mongoose for MongoDB。为了达到预期的结果,您需要正确链接

populate
方法并使用返回的 Promise。以下是如何填充
createdBy
对象内的
fileInfo
字段的示例:

Document.findOne({ _id: new mongoose.Types.ObjectId(documentId) })
  .populate('labels')
  .populate({
    path: 'fileInfo.createdBy',
    model: 'User',
    select: 'name isSupportUser isActivated isSubscribed activateBttnClickCount primarySupport',  //  you can select proper field
  })
  .then(function (documentView) {
    console.log(documentView, 'documentView');  // actually if with no this part, can be worked but it is used for safety 
    return next(null, documentView);
  })
  .catch((error) => {
    console.error(error);
    return next(error);
  });

此示例对标签和

populate
字段使用
fileInfo.createdBy
方法。另外,请确保处理代码中的错误,以获得更稳健的实现。

注意:第二个

select
调用中的
populate
选项用于指定要从
User
模型中检索的字段。根据您需要的字段进行调整。

© www.soinside.com 2019 - 2024. All rights reserved.