Home > Net >  how to get the value of mongodb ObjectId?
how to get the value of mongodb ObjectId?

Time:01-13

I am new to mongodb and I am trying to get a Documment from the database and populate some fields this is an example :

var profile = await this.profileModel
  .findById(id)
  .populate({
      path: 'posts',
      populate: {
          path: 'comments',
          populate: [
                  { path: 'author', select: ['_id', 'firstName', 'lastName', 'email', 'image', 'status'] },
                    ],
                },
            })
            .lean();

the problem here is that _id for comments and author are objectId and I want to get their values

how can I achieve this ?

CodePudding user response:

const myObjectIdValue = ObjectId.valueOf();

this method returns the str attribute of the ObjectId : An hexadecimal string representation of the object.

learn more here in the official docs

  • Related