前端 ----MongoDB

 

database:数据库,mongoDB数据库软件中可以建立多个数据库
collection:集合,一组数据的集合,可以理解为JavaScript中的数组
document:文档,一条具体的数据,可以理解为JavaScript中的对象
field:字段,文档中的属性名称,可以理解为JavaScript中的对象属性

一、使用
  1.启动MongoDB
  在命令行工具中运行net start mongoDB即可启动MongoDB,否则MongoDB将无法连接。
  导入数据:mongoimport –d 数据库名称 –c 集合名称 –file 要导入的数据文件

  2.数据库连接
    mongoose.connect('mongodb://localhost/playground')
    .then(() => console.log('数据库连接成功'))
    .catch(err => console.log('数据库连接失败', err));
  3.创建数据库
    在MongoDB中如果正在使用的数据库不存在,MongoDB会自动创建。
    --如果你创建了一个数据库但里面没有数据,那么就在mongodb compass里看不到这个数据库
  4.创建集合
    创建集合分为两步,一是对对集合设定规则,二是创建集合,创建mongoose.Schema构造函数的实例即可创建集合。

1 // 设定集合规则
2 const courseSchema = new mongoose.Schema({
3     name: String,
4     author: String,
5     isPublished: Boolean
6     });
7 // 创建集合并应用规则
8 const Course = mongoose.model('Course', courseSchema); // courses

  5.创建文档
    创建文档实际上就是向集合中插入数据。
    分为两步:
      1.创建集合实例。
      2.调用实例对象下的save方法将数据保存到数据库中。
  // 方法1:创建集合实例并插入数据

const course = new Course({
    name: 'Node.js course',
    author: '黑马讲师',
    tags: ['node', 'backend'],
    isPublished: true
    });
// 将数据保存到数据库中
course.save();

  //方法2.往创建的集合插入数据
  

Course.create({name: 'JavaScript基础', author: '黑马讲师', isPublish: true}, (err, doc) => { 
// 错误对象
console.log(err)
// 当前插入的文档
console.log(doc)
});

  //方法3:
    

Course.create({name: 'JavaScript基础', author: '黑马讲师', isPublish: true})
.then(doc => console.log(doc))
.catch(err => console.log(err))

  6.查询文档
    1.// 根据条件查找文档(条件为空则查找所有文档)
      Course.find().then(result => console.log(result))
    2.// 根据条件查找文档
      Course.findOne({name: 'node.js基础'}).then(result => console.log(result))
    3.// 匹配大于 小于
      User.find({age: {$gt: 20, $lt: 50}}).then(result => console.log(result))
    4. // 匹配包含
      User.find({hobbies: {$in: ['敲代码']}}).then(result => console.log(result))
    5.// 选择要查询的字段
      User.find().select('name email').then(result => console.log(result))
    6. // 将数据按照年龄进行排序
      User.find().sort('age').then(result => console.log(result))
    7.// skip 跳过多少条数据 limit 限制查询数量
      User.find().skip(2).limit(2).then(result => console.log(result))

  7.删除文档
    // 删除单个
    Course.findOneAndDelete({}).then(result => console.log(result))
    // 删除多个
    User.deleteMany({}).then(result => console.log(result))

  8.更新文档
    // 更新单个
    User.updateOne({查询条件}, {要修改的值}).then(result => console.log(result))
    // 更新多个
    User.updateMany({查询条件}, {要更改的值}).then(result => console.log(result))

  9.集合关联实现
    // 用户集合
      const User = mongoose.model('User', new mongoose.Schema({ name: { type: String } }));
    // 文章集合
      const Post = mongoose.model('Post', new mongoose.Schema({
      title: { type: String },
    // 使用ID将文章集合和作者集合进行关联
      author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
      }));
    //联合查询
      Post.find()
      .populate('author')
      .then((err, result) => console.log(result));


  10.mongoose验证
    在创建集合规则时,可以设置当前字段的验证规则,验证失败就则输入插入失败。
    获取错误信息:error.errors['字段名称'].message

required: true 必传字段
minlength:3 字符串最小长度
maxlength: 20 字符串最大长度
min: 2 数值最小为2
max: 100 数值最大为100
enum: ['html', 'css', 'javascript', 'node.js']
trim: true 去除字符串两边的空格
validate: 自定义验证器
default: 默认值

如果没有条件就是所有(删除,更新)


301是重定向的状态
res.writeHead(301,{
Location:'/list'//跳转路径
})

posted @ 2020-08-16 11:17  otome  阅读(184)  评论(0编辑  收藏  举报