const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/student', { useNewUrlParser: true, useUnifiedTopology: true });
const userSchema = mongoose.Schema({
username: {
type: String,
required: true
},
password: {
type: String,
required: true
}
});
const user = mongoose.model('User', userSchema);
const admin = new user({
username: 'zhangsan',
password: '123456'
});
admin.save(function(err, res) {
if (err) {
console.log(err)
} else {
console.log(res)
}
})
user.find({ username: 'zhangsan' }, function(err, res) {
if (err) {
console.log(err)
} else {
console.log(res)
}
})
user.find(function(err, res) {
if (err) {
console.log(err)
} else {
console.log(res)
}
})
user.findOne({ username: 'zhangsan' }, function(err, res) {
if (err) {
console.log(err)
} else {
console.log(res)
}
})
user.remove({ username: 'lisi' }, function(err, res) {
if (err) {
console.log(err)
} else {
console.log('success');
console.log(res)
}
})
user.findByIdAndUpdate('5e7cc0b1f093166c5490045b', {
password: 'admin'
}, function(err, res) {
if (err) {
console.log(err)
} else {
console.log('success');
console.log(res)
}
})
user.updateMany({
_id: '5e7cc0b1f093166c5490045b'
}, {
username: 'zhangsan3',
password: 'admin'
}, function(err, res) {
if (err) {
console.log(err)
} else {
console.log('success');
console.log(res)
}
})
nodejs连接mongodb(进阶)
最新推荐文章于 2023-01-26 09:04:13 发布