How to populate a populated field name in mongoose

71 Views Asked by At

this is my example populated result:

I need to populate the popoluated field name from 'fileInfo' Inside fileInfo i need to populate the filed name '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
}

I want to populate the 'createdBy' field inside the 'fileInfo' object.

the 'createdBy' field is in User model. this is Document model

I have tried this and not getting the result


      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);
        })

This is what i expect.


{
  _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
}

This is the Document schema below


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,
});

Below code is the Attachment schema


{
    "_id" : ObjectId("656964dae706b9ca20b71973"),
    "name" : "7499.pdf",
    "size" : 251058,
    "resourceKey" : "VRHaTwOr94IR4j3CdOn1c_7499.pdf",
    "resourcePath" : "655c377de5a501b11cbf5d99/documents",
    "numberOfPages" : 1,
    "accountId" : ObjectId("655c377de5a501b11cbf5d99"),
    "createdOn" : ISODate("2023-12-01T04:45:14.650Z"),
    "modifiedOn" : ISODate("2023-12-01T04:45:14.650Z"),
    "createdBy" : ObjectId("655c377de5a501b11cbf5d94"),
    "modifiedBy" : ObjectId("655c377de5a501b11cbf5d94"),
    "fileType" : "application/pdf",
    "__v" : 0
}

Thanks in advance

0

There are 0 best solutions below