mongodb学习笔记(3)--mongoose实现curd
简介
mongoose实现curd
初始化
npm init cnpm install mongoose --save-dev
index.js
const mongoose = require('mongoose'); mongoose.connect('mongodb://127.0.0.1:27017/test97'); const UserSchema = mongoose.Schema({ name:String, age:Number, create_at:{ type:Date, default:Date.now() }, isShow:{ type:Boolean, default:true } }) const User = mongoose.model('User',UserSchema,'user'); module.exports = User;
inser.js
const User = require('./index.js'); let newUser = new User({ name:"jie", age:20, }) newUser.save().then(result => { console.log(result) }) .catch(error => { console.log(error) })
find.js
const User = require('./index.js'); User.find().then(result => { console.log(result) }) .catch(error => { console.log(error) })
update.js
const User = require('./index.js'); User.updateOne({"name":"biao"},{ age:300 }).then(result => { console.log(result) }) .catch(error => { console.log(error) })
delete.js
const User = require('./index.js'); User.deleteOne({"name":"biao"}).then(result => { console.log(result) }) .catch(error => { console.log(error) })
相关推荐
80530895 2020-07-05
mkhhxxttxs 2020-06-14
80500495 2020-06-14
86211943 2020-03-01
80530895 2020-02-23
80500495 2020-01-29
86211943 2020-01-24
lovecodeblog 2020-01-24
87261046 2019-12-23
86211943 2019-12-20
lightlanguage 2019-12-16
MYRENZHIBO 2019-08-28
85234656 2018-09-03
fudirong 2012-04-12
80500495 2019-07-01