Docs: https://mongoosejs.com/docs/
NPM: https://www.npmjs.com/package/mongoose
查询
验证字段
async function main() {
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/ajanuw', {
useNewUrlParser: true
})
const Schema = mongoose.Schema
const UserSchema = new Schema({
name: String,
age: Number,
qq: Number,
phone: Number,
hobby: Array,
})
UserSchema.methods.hello = function () {
console.log(`我叫${this.name}, 喜欢${this.hobby[0]}~`);
}
const User = mongoose.model('users', UserSchema)
let user = new User({
name: 'alone',
age: 12,
qq: 1324055400,
phone: 15281414663,
hobby: ['game', 'code'],
dec: '什么都没哟呀'
})
// console.log(user)
user.hello()
await user.save() //在表中保存该文档
}
main();