zoukankan      html  css  js  c++  java
  • MongoDB的基本用法

    数据库相关概念

    在一个数据库软件中可以包含多个数据仓库,在每个数据仓库中可以包含多个数据集合,每个数据集合中可以包含多条文档(具体的数据)

    术语:

    database 数据库,mongoDB 数据库阮家中可以建立多个数据库

    collection  集合,一组数据的集合,可以理解为JavaScript中的数组

    document 文档,一条具体的数据,可以理解为JavaScript中的对象

    filed 字段,文档中的属性名称,可以理解为javascript 中的对象属性

    操作数据库

    使用node.js 操作数据库需要依赖Node.js 的第三方包 mongoose

    运行安装命令

    npm i mongoose

    连接数据库

    1 const mongoose = require('mongoose')
    2 
    3 mongoose.connect('mongodb://localhost/test1', { useNewUrlParser: true })
    4 .then(() => console.log('数据库连接成功'))
    5 .catch( err => console.log('数据连接失败' + err))

      

    创建数据库

    在MongoDB 中不需要显式创建数据库,如果正在使用的数据不存在,MongoDB 会自动创建。

    增删改查操作

    创建集合:

     创建集合分为两步,一是对集合设定规则,二是创建集合,创建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)

    创建文档:

    创建文档实际上就是向集合中插入数据。

    分为两步:1)创建集合实例。2)调用实例对象下的save方法将数据保存到数据库当中。

    1 // 插入数据
    2 const course = new Course ({
    3   name: 'node',
    4   author: 'zhangsan',
    5   isPublished: true
    6 })
    7 course.save()

    向数据库导入数据

    命令: mongoimport -d 数据库名称 -c 集合名称 --flile 要导入的数据文件

    在导入数据之前,需要确保mongodb 已经设置了环境变量,如果没有设置,需要把mongodb/bin 目录添加到系统环境中。在设置好环境变量之后重启,就可以使用命令行执行操作了。

    示例:

    mongoimport -d test1 -c uesr1 --file  ./user.json
    

    查询文档

    查找所有数据

    xxx.find() 方法: 返回一组符合条件的数据

    xxx.find({查找条件}).then( res => console.log( res )

    // 查询集合中所有文档
    // Course.find().then(res => console.log('查询结果' + res))
    
    // 根据 _id  查找文档
    Course.find({ _id: '5d343c27d201764240160daf' }).then(res => console.log('查询结果' +res))
    

    查询一个数据

    xxx.findOne() 方法: 返回一个文档 

    //  xxx.findOne() 方法 返回一条文档,默认返回的是第一条文档,()中可以添加查找条件
    Course.findOne().then( res => console.log(res))
    
    Course.findOne({name: 'node'}).then( res => console.log( res ))
    

    条件匹配查询

    xxx.find()

    // 匹配数值范围 $gt 大于的意思, $lt 小于的意思, 查询 User 文档中 age 字段 大于 20 小于 50 的数据
    User.find({ age: { $gt: 20, $lt: 50 } }).then(res => console.log(res))
    
    // 匹配包含  $in 是 匹配包含的意思
    User.find( {hobbies: { $in: ['足球'] }}).then(res => console.log(res))
    
    // 选择要查询的字段  (只查询 name 和 email 字段 并排除 _id 字段 ) -字段名 可以排除查询到的字段
    User.find().select( 'name email -_id').then( res => console.log(res))
    
    // 将数据按照 age 字段 进行升序排列
    User.find().sort('age').then( res => console.log(res) )
    
    // 将数据按照 age 字段 进行降序排列 字段前面加 - 表示降序排列
    User.find().sort('-age').then( res => console.log(res) )
    
    //  skip 跳过多少条数据 limit 限制查询数量
    User.find().skip(2).limit(5).then( res => console.log(res))
    

    删除文档

    const mongoose = require('mongoose')
    
    mongoose.connect('mongodb://localhost/test1', { useNewUrlParser: true })
    .then(() => console.log('数据库连接成功'))
    .catch( err => console.log('数据连接失败' + err))
    
    // 设定集合规则
    const courseSchema = new mongoose.Schema({
      name: String,
      author: String,
      isPublished: Boolean
    })
    // 创建集合并应用规则
    const Course = mongoose.model('Course', courseSchema)
    
    // 删除一个
    Course.findOneAndDelete({_id: '5d343ab3981298416bd26998'}).then( res => console.log(res))
    
    // 删除多条文档  不添加条件默认删除所有文档,返回值 ok 等于 表示删除成功, n 代表删除数据的条数
    Course.deleteMany({}).then(res => console.log(res))
    

    修改文档

    const mongoose = require('mongoose')
    
    mongoose.connect('mongodb://localhost/test1', { useNewUrlParser: true })
    .then(() => console.log('数据库连接成功'))
    .catch( err => console.log('数据连接失败' + err))
    
    // 设定集合规则
    const courseSchema = new mongoose.Schema({
      name: String,
      author: String,
      isPublished: Boolean
    })
    // 创建集合并应用规则
    const Course = mongoose.model('Course', courseSchema)
    
    // 修改单个
    // xxx.updateOne({查询条件}, {要修改的值}).then(res => console.log(res))
    // 修改文档的一个字段, updataOne() 里面需要传两个参数,第一是要修改的字段,第二个修改成的内容
    Course.updateOne({author: 'lisi'},  {author: 'wangwu'}).then(res => console.log(res))
    
    // 修改多个 查询条件不写,则修改所有
    // xxx.updateMany({查询条件}, {要修改的值}).then( res => console.log(res))
    Course.updateMany({}, {name: 'javascript'}).then( res => console.log(res))
    
  • 相关阅读:
    使用Anaconda安装TensorFlow
    更新pip源/anaconda源
    PHP 中 config.m4 的探索
    有趣的智力题
    工作中MySql的了解到的小技巧
    一篇关于PHP性能的文章
    eslasticsearch操作集锦
    curl 命令详解~~
    Nginx 调优经验记录
    Elasticsearch安装使用
  • 原文地址:https://www.cnblogs.com/liea/p/11221346.html
Copyright © 2011-2022 走看看