const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/2022132171');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("ok")
});
var UserSchema = mongoose.Schema({
name: String,
age: Number,
})
var User = mongoose.model('User', UserSchema, 'user');
//增加
// var small = new User({name:"王五",age:25});
// small.save()
//删除
// User.deleteOne({name:"张三"},(err,rs02)=>console.log(rs02))
//修改
User.findOneAndUpdate({name:"李四"},{name:"张三"},{new:true},(err,rs) => console.log(rs))
//查询
User.findOne({ name:"张三" },function (err,result) {
err ? console.log('没有数据' + err) : console.log(result)
})
6.18mongoon实现简单增删改查
于 2024-07-02 07:53:50 首次发布