zoukankan      html  css  js  c++  java
  • 数据库使用

    1.数据库相关概念

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

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

    2. Mongoose第3三方包

    • 使用Nodejs操作MongoDB数据库需要依赖Node.js第 三方包mongoose

    • 使用npm install mongoose命令下载

    3.启动MongoDB

    在管理员命令行工具中运行net start mongoDB即可启动MongoDB,否则MongoDB将无法连接。

    4.数据库连接

    使用mongoose提供的connect方法即可连接数据库。

    MongoDB返回的是promise对象

    mongoose.connect('mongodb://localhost/playground')
        .then(() => console.log('数据库连接成功'))
        .catch(err => console.log('数据库连接失败', err));

    连接数据库时如果提示如下信息,在content方法里面添加第二个参数, { useNewUrlParser: true }

    (node:15596) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

    如果提示(node:14524) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

    则继续添加{ useUnifiedTopology: true },用逗号隔开

    // 引入第三方模块mongoose
    const mongoose = require('mongoose');
    // 1、连接数据库playground,如果没有此数据库,系统会自动创建
    mongoose.connect('mongodb://localhost/playground', {
           useUnifiedTopology: true,
           useNewUrlParser: true
      })
       // 连接成功
      .then(() => console.log('数据库连接成功'))
       // 连接失败
      .catch(err => console.log(err, '数据库连接失败'));

    5. 创建数据库

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

     

  • 相关阅读:
    PO BO VO DTO POJO DAO DO这些Java中的概念分别指一些什么?
    前端面试题汇总(待续)
    vue lottie vue-lottie : 使用教程
    webstorm 换行时 代码不对齐
    webstorm 导出编辑器配置.editorconfig
    vue 查看dist文件里的结构
    vue-cli 生产打包
    element form 校验数组每一项
    typescript无法识别vue中的$refs
    mac 10.14.5 [vue create的时候 mkdir没有权限]
  • 原文地址:https://www.cnblogs.com/xc-dh/p/13917183.html
Copyright © 2011-2022 走看看