1 创建数据
1.1 连接数据库
1 2 3 4 5 6 7 8 9 10 11
| const mongoose = require('mongoose')
mongoose .connect('mongodb://用户名:密码@主机:端口/数据库名', { useNewUrlParser: true, }) .then(() => console.log('数据库连接成功')) .catch((err) => console.log(err, '数据库连接失败'))
|
1.2 创建集合
1 2 3 4 5 6 7 8 9 10
| const userSchema = new mongoose.Schema({ name: String, age: String, })
const User = mongoose.model('User', userSchema)
|
1.3 创建文档
方式一,实例化:
1 2 3 4 5 6 7
| const user = new User({ name: 'Tom', age: '22', })
user.save()
|
方式二,creat 函数(异步):
1 2 3 4
| User.create({ name: 'Sam', age: '23' }).then((result) => { console.log(result) })
|
或
1 2 3 4 5
| User.create({ name: 'Alice', age: '21' }, (err, result) => { console.log(err) console.log(result) })
|
1.4 导入数据
命令行输入:
1
| mongoimport –d 数据库名称 –c 集合名称 –-file 要导入的数据文件
|
mongoimport 在 mongodb 安装目录下
2 文档查删改
2.1 文档查找
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
| User.find().then((result) => console.log(result))
User.distinct('name').then((result) => console.log(result))
User.find({ name: /张/ }).then((result) => console.log(result))
User.find({ name: '张三', age: 20 }).then((result) => console.log(result))
User.find({ $or: [{ age: 20 }, { age: 32 }] }).then((result) => console.log(result) )
User.find({ _id: '5c09f267aeb04b22f8460968' }).then((result) => console.log(result) )
User.findOne({ name: '李四' }).then((result) => console.log(result))
User.find({ age: { $gt: 20, $lte: 40 } }).then((result) => console.log(result))
User.find({ hobbies: { $in: ['足球'] } }).then((result) => console.log(result))
User.find() .select('name email -_id') .then((result) => console.log(result))
User.find() .sort('age') .then((result) => console.log(result))
User.find() .sort('-age') .then((result) => console.log(result))
User.find({ age: { $gte: 25 } }) .count() .then((result) => console.log(result))
User.find() .skip(2) .limit(3) .then((result) => console.log(result))
|
2.2 文档删除
1 2 3 4 5 6 7 8 9
|
User.findOneAndDelete({ _id: '5c09f267aeb04b22f8460968' }).then((result) => console.log(result) )
User.deleteMany({}).then((result) => console.log(result))
|
2.3 文档更新
1 2 3 4 5 6 7
| User.updateOne({ name: '李四' }, { age: 120, name: '李狗蛋' }).then((result) => console.log(result) )
User.updateMany({}, { age: 300 }).then((result) => console.log(result))
|
3 集合验证
验证字段:
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
| const postSchema = new mongoose.Schema({ title: { type: String, required: [true, '请传入文章标题'], minlength: [2, '文章长度不能小于2'], maxlength: [5, '文章长度最大不能超过5'], trim: true, }, age: { type: Number, min: 18, max: 100, }, publishDate: { type: Date, default: Date.now, }, category: { type: String, enum: { values: ['html', 'css', 'javascript', 'node.js'], message: '分类名称要在一定的范围内才可以', }, }, author: { type: String, validate: { validator: (v) => { return v && v.length > 4 }, message: '传入的值不符合验证规则', }, }, })
|
错误捕捉:
1 2 3 4 5 6 7 8 9 10 11 12 13
| const Post = mongoose.model('Post', postSchema)
Post.create({ title: 'aa', age: 60, category: 'java', author: 'bd' }) .then((result) => console.log(result)) .catch((error) => { const err = error.errors for (var attr in err) { console.log(err[attr]['message']) } })
|
4 集合关联
建立关联:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| const userSchema = new mongoose.Schema({ name: { type: String, required: true, }, })
const postSchema = new mongoose.Schema({ title: { type: String, }, author: { type: mongoose.Schema.Types.ObjectId, ref: 'User', }, })
|
查询关联:
1 2 3
| Post.find() .populate('author') .then((result) => console.log(result))
|
最后更新时间:
转载请注明出处