0

I have two different types of users collection (googleUser & localUser ). How can I check if these collections have the same email ?

Here is the schema of both the collections

let googleUser = mongoose.Schema({
id:{
  type:String,
  default:null
},
username: String,
first_name : {type:String, required: [true, "first_name required"]},
last_name : String,
picture:String,
email:{
  type:String,
  required:[true,"Email required"],
  unique:[true,"Email already registered"]
}, 
provider:{ type:String,required:true },
last_Visited : { type: Date, default: new Date()},
joined_At : { type: Date, default: new Date()},
});




let localUser= mongoose.Schema({
username: {type:String,unique:true,required: [true, "username required"] },
first_name : {type:String,required: [true, "firstName required"] },
last_name : String,
password: { type: String,minLength:8, required: [true, "password required"] },
picture: String,
bio:String,
email: {
  type: String,
  required: [true, "Email required"],
  unique: [true, "Email already registered"],
},
provider: { type: String, required: true },
last_Visited: { type: Date, default: new Date() },
joined_At: { type: Date, default: new Date() },
});


Prince
  • 1
  • 1

1 Answers1

0

You have to do two separate checks, like so:

let user1 = await googleUser.find({email: 'email'});
let user2 = await localUser.find({email: 'email'});

// If both returns the user, both have the same email.
if (user1 && user2) {
    // ...
}
Hamza
  • 469
  • 3
  • 9
  • Bro is there any built-in method to do this ? – Prince Aug 11 '22 at 23:19
  • Do you mean search both schemas at the same time? check this it might help: https://stackoverflow.com/questions/20056903/search-on-multiple-collections-in-mongodb – Hamza Aug 12 '22 at 00:28