1

I have the two mongoose schemas where one schema reference the other. For example I have

const loginsSchema = new mongoose.Schema({

displayName:{
    type:String,
    required:true,

},
serviceId:{
    required:true,
    type:String,
    unique:true
 }
})
const logins = mongoose.model('logins',loginsSchema);

module.exports = logins;
const usersSchema = new mongoose.Schema({
userName: {
    type: String,
    required: true,
},
email: {
    type: String,
    unique: true,

   },    
    user: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
    ref: 'logins',
    unique: true
}
})

What I would like to do is to create a new schema and reference the userSchema in it but not with the objectid. I would like to use the email property.

For Example

const usersSchema = new mongoose.Schema({
name1: {
  type: String,
  required: true,
},
name2: {
  type: String,
  unique: true,

},    
emailref: {
    /// reference the username schema with the email property instead of the objectId
  }
 })

How can I do that in mongoose?

Ahmed
  • 1,229
  • 2
  • 20
  • 45
  • Alternatively, you can try out Population of virtuals https://mongoosejs.com/docs/populate.html#populate-virtuals – abondoa Apr 10 '20 at 00:21
  • Does this answer your question? [Using SchemaTypes other than ObjectId for Population (Mongoose)](https://stackoverflow.com/questions/23497614/using-schematypes-other-than-objectid-for-population-mongoose) – abondoa Apr 10 '20 at 00:22
  • no these doesnot answer my question – Ahmed Apr 10 '20 at 00:45

0 Answers0