Check if ID exists in a collection with mongoose

前端 未结 5 936
無奈伤痛
無奈伤痛 2021-01-31 15:52

For instance, I have a collection User:

var mongoose = require(\'mongoose\');

var UserSchema = new mongoose.Schema({
    email: String,
    googleI         


        
5条回答
  •  梦毁少年i
    2021-01-31 16:30

    OR you can simply use exists function, without making any async/await:

    myData = {_id: userID};
    
    User.exists(myData,(error, result)=>{
        if (error){
          console.log(error)
        } else {
          console.log("result:", result)  //result is true if myData already exists
        }
      });
    

    You can play with the result now!

提交回复
热议问题