简体   繁体   中英

Get data from database using a findOne() in mongoose

I have this schema:

const statsData = new mongoose.Schema({
  userID: {type: String},
  warned: {type: Number, default:0},
  warns: {type: Number, default:0},
  muted: {type: Number, default:0},
  mutes: {type: Number, default:0},
  banned: {type: Number, default:0},
  bans: {type: Number, default:0},
  aWarned: {type: Number, default:0},
  aWarns: {type: Number, default:0},
  give: {type: Number, default:0}
})

and this model

const statsModle = mongo.model("statsModle", statsData)

I need to get the data by using the userID above one each time.

YourSchema.fineOne({ _id: userID }, (err, foundUser) => {
    // foundUser is the data you want
})

If you want to fetch the document via an id you can even use a simpler version:

YourSchema.findById(userID, (err, foundUser) => {
    // ...
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM