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会自动创建

     

  • 相关阅读:
    Sql Server截断日志(转)
    C#/VB.NET语法的比较(转)
    ReportViewer矩阵报表
    逐步学习 iPhone App 开发(1)
    一月二十四日,无家可归
    再见2009
    poj 1376 机器人广搜
    hdu 1004 颜色问题
    hdu 2734 简单地字符串处理
    1116 贪婪的送礼者
  • 原文地址:https://www.cnblogs.com/xc-dh/p/13917183.html
Copyright © 2011-2022 走看看