1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true } )
const mongooseConnection = mongoose.connection
mongooseConnection.on('connected', function () { console.log('Mongo数据库连接成功') })
const userSchema = mongoose.Schema({ username: { type: String, required: true }, password: { type: String, required: true }, userType: { type: String, required: true }, name: { type: String }, avatar: { type: String }, position: { type: String }, personalInfo: { type: String }, companyInfo: { type: String }, salary: { type: String } })
const chatSchema = mongoose.Schema({ from_id: { type: String, required: true }, to_id: { type: String, required: true }, chat_id: { type: String, required: true }, content: { type: String, required: true }, hasRead: { type: Boolean, default: false }, create_time: { type: String }, })
function saveTest () { const userModel = new UserModel({ username: 'test', password: md5('654321'), type: 'jobSeeker' }) userModel.save((err, userDoc) => { if (err){ console.log(err) } else { console.log(userDoc) }
}) } saveTest()
function findTest () { UserModel.find((err, usersDocArr) => { if (err) { console.log(err) } else { console.log(usersDocArr) } }) UserModel.findOne({ _id: '5f32912c5a24d012779cf1ea' }, (err, userDoc) => { if (err) { console.log(err) } else { console.log(userDoc) } }) }
findTest() function updateTest () { UserModel.findByIdAndUpdate( {_id: '5f329d9fb913dd12e4d67a0a'}, {username: 'test-update'}, {useFindAndModify:false}, (err,oldUserDoc) => { if (err) { console.log(err) } else { console.log(oldUserDoc) } })
} updateTest()
function deleteTest () { UserModel.deleteOne( {_id: '5f329d9fb913dd12e4d67a0a'}, (err,delInfo) =>{ if (err) { console.log(err) } else { console.log(delInfo) } }) }
deleteTest()
|