1

I want to populate the adminId path to User Model.
Here is the code

adminInfo: {
    _id: false,
    adminId: [{
      type: Schema.Types.ObjectId,
      ref: 'User'
    }]
 }

Here is a part of user schema:

// user schema
const UserSchema = new mongoose.Schema({
  name: {
    firstName: {
      type: String,
      trim: true,
      required: true,
    },
    lastName: {
      type: String,
      trim: true
    }
  },
  email: {
    type: String,
    trim: true,
    required: true,
    unique: true,
    lowercase: true
  },
  phone: {
    type: String,
    trim: true,
    minlength: 10,
  },
  password: {
    type: String,
    trim: true,
    required: true,
    minlength: 6
  }
});

I have tried using .populate('adminInfo.adminId') but it's giving empty array [] whereas .populate('adminInfo') giving array of admins ids but not getting populated to User model

6
  • Hi! Please add the User schema to the question Commented Jun 23, 2020 at 18:05
  • Hi @Tunmee updated! Commented Jun 23, 2020 at 18:27
  • I thought the adminInfo property is a part of the User schema, from your latest edit, I can see it's not. Can you also share the schema where the adminInfo property exists Commented Jun 23, 2020 at 18:33
  • @Tunmee If adminInfo property will be a part of User schema then why I will refer that User schema?? adminInfo property is a part of admin schema from that I want to populate adminId to User schema Commented Jun 23, 2020 at 18:40
  • Can you also share the admin schema Commented Jun 23, 2020 at 18:43

1 Answer 1

0

i don't think there is any problem with .populate('adminInfo.adminId') method.

are you sure that ref field is in CamelCase .

If not, try to change ref field ->

adminInfo: {
  _id: false,
  adminId: [{
    type: Schema.Types.ObjectId,
    ref: 'user'
  }]
}
Sign up to request clarification or add additional context in comments.

1 Comment

yes, I am sure, also it works when I am changing schena to : adminId:[ { type: Schema.Types.ObjectId, ref: 'User' }] and then .populate('adminId')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.