const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/demo')
const Schema = mongoose.Schema
const userSchema = new Schema({
username: {
type: String,
required: true
},
password: {
type: String,
required: true
},
email: {
type: String
}
})
const User = mongoose.model('User', userSchema)
// User.find()
// .then(function(data){
// console.log(data)
// })
// 注册用户小demo
User.findOne({
username: '999'
}).then(user => {
if (user) {
console.log('该用户名已经存在')
} else {
return new User({
username: '999',
password: '123',
email: '999@qq.com'
})
.save()
.then(ret => {
console.log('注册成功', ret)
})
}
})