数据库操作
1 添加数据
// 第三方模块操作数据库
const mongoose = require('mongoose');
// 连接数据库
mongoose.connect('mongodb://localhost/school', { useNewUrlParser: true, useUnifiedTopology: true }).then(() => {
console.log('数据库连接成功');
}).catch(err => {
console.log(err);
});
// 1 创建集合规则
const teacherSchema = mongoose.Schema({
name: String,
tel: String,
age: Number
});
// 2 创建符合规则的集合实例
const Teacher = mongoose.model('Teacher', teacherSchema);
// 3 创建文档
// 方法一
// const teacher = new Teacher({
// name: 'ziran',
// tel: '12345678901',
// age: 33
// });
// 4 文档插入到数据库 后面方法不需要此操作
// teacher.save();
// 方法二
// Teacher.create({
// name: 'lucy',
// age: 26,
// tel: '1356897845'
// }, (err, doc) => {
// console.log(err);
// console.log(doc); // doc就是数据库的文档
// });
//
Teacher.create({
name: 'lili',
age: 26,
tel: '1356897845'
}).then(doc => console.log(doc)).catch(err => console.log(err));
// 方法三 命令导入整个文件
// mongoimport -d school -c students --file D:\01-Web\node-study\mongodb\student.json
2 查询数据
// 第三方模块 操作数据库
let mongoose = require('mongoose');
// 链接数据库
mongoose.connect('mongodb://localhost/school', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('数据库连接成功');
}).catch(err => console.log(err));
// 1 创建集合规则
const studentSchema = mongoose.Schema({
name: String,
age: Number,
email: String,
hobbies: [String],
qq: String
});
// 2 创建符合规则的集合实例
const Student = mongoose.model('Student', studentSchema);
// *** 查询集合中所有的文档
//Student.find().then(res => console.log(res));
// *** 根据指定字段查询
//Student.find({ _id: '5e4a988aa1f38717641911f4' }).then(res => console.log(res));
//Student.find({ name: 'zs' }).then(res => console.log(res));
// *** 默认返回集合中满足条件的第一条文档
//Student.findOne({ name: 'zs' }).then(res => console.log(res));
// *** 查找年龄18-30之间的学生 大于等于,小于等于
// Student.find({ age: { $gte: 18, $lte: 30 } }).then(res => console.log(res));
// *** 查找爱好中包含篮球的学生
//Student.find({ hobbies: { $in: ['篮球'] } }).then(res => console.log(res));
// *** 查找所有学生的年龄和姓名 -_id 不查询_id 字段
//Student.find().select('name age -_id').then(res => console.log(res));
// *** 按年龄升序
//Student.find().sort('age').then(res => console.log(res));
// *** 按年龄降序
//Student.find().sort('-age').then(res => console.log(res));
// *** 跳过某些数据
// 跳过前两条
Student.find().skip(2).then(res => console.log(res));
// 跳过两个,只显示两条
Student.find().skip(2).limit(2).then(res => console.log(res));
3 删除&修改数据
// 第三方模块 操作数据库
let mongoose = require('mongoose');
// 链接数据库
mongoose.connect('mongodb://localhost/school', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('数据库连接成功');
}).catch(err => console.log(err));
// 1 创建集合规则
const studentSchema = mongoose.Schema({
name: String,
age: Number,
email: String,
hobbies: [String],
qq: String
});
// 2 创建符合规则的集合实例
const Student = mongoose.model('Student', studentSchema);
// 删除zs 删除第一条
//Student.findOneAndDelete({ name: 'zs' })
// .then(res => console.log(res)).catch(e => console.log(e));
// Student.findOneAndDelete({ _id: '5e4a988aa1f38717641911f5' })
// .then(res => console.log(res)).catch(e => console.log(e));
// 删除多条数据
// Student.deleteMany({ name: 'zs' })
// .then(res => console.log(res)).catch(e => console.log(e));
// 将nana的年龄修改为18
//Student.updateOne({ name: 'nana' }, { age: 18, email: 'nana@163.com' }).then(res => console.log(res));
Student.updateMany({}, { age: 30 }).then(res => console.log(res));