zoukankan      html  css  js  c++  java
  • nodejs连接mongodb实现数据CRUD

    const mongoose = require('mongoose')

    const Schema = mongoose.Schema

    // 1. 连接数据库
    // 指定连接的数据库不需要存在,当你插入第一条数据之后就会自动被创建出来
    mongoose.connect('mongodb://localhost/test', {useMongoClient: true})

    // 2. 设计文档结构(表结构)
    // 字段名称就是表结构中的属性名称
    // 约束的目的是为了保证数据的完整性,不要有脏数据
    const userSchema = new Schema({
    nickname: {
    type: String,
    required: true //必须有
    },
    email: {
    type: String,
    required: true
    },
    password: {
    type: String,
    required: true
    },
    created_time: {
    type: Date,
    // 注意:这里不要写 Date.now() 因为会即刻调用
    // 这里直接给了一个方法:Date.now
    // 当你去 new Model 的时候,如果你没有传递 create_time ,则 mongoose 就会调用 default 指定的Date.now 方法,使用其返回值作为默认值
    default: Date.now
    },
    last_modified_time: {
    type: Date,
    default: Date.now
    },
    avatar: {
    type: String,
    default: '/images/avatar-default.png'
    },
    gender: {
    type: Number,
    enum: [-1, 0, 1],
    default: -1
    },
    birthday: {
    type: Date
    },
    status: {
    type: Number,
    // 0 没有权限限制
    // 1 不可以评论
    // 2 不可以登录
    enum: [0, 1, 2],
    default: 0
    }
    })

    // 3. 将文档结构发布为模型
    // mongoose.model 方法就是用来将一个架构发布为 model
    // 第一个参数:传入一个大写名词单数字符串用来表示你的数据库名称
    // mongoose 会自动将大写名词的字符串生成 小写复数 的集合名称
    // 例如这里的 User 最终会变为 users 集合名称
    // 第二个参数:架构 Schema
    //
    // 返回值:模型构造函数
    const User = mongoose.model('User', userSchema)


    // 4. 当我们有了模型构造函数之后,就可以使用这个构造函数对 users 集合中的数据增删改查
    // **********************
    // #region /新增数据
    // **********************
    let admin = new User({
    username: 'zs',
    password: '123456',
    email: 'admin@admin.com'
    })

    admin.save(function (err, ret) {
    if (err) {
    console.log('保存失败')
    } else {
    console.log('保存成功')
    console.log(ret)
    }
    })
    // **********************
    // #endregion /新增数据
    // **********************


    // **********************
    // #region /查询数据
    // **********************

    User.find({
    username: 'zs'
    }, function (err, ret) {
    if (err) {
    console.log('查询失败')
    } else {
    console.log(ret)
    }
    })

    User.findOne({
    username: 'zs'
    }, function (err, ret) {
    if (err) {
    console.log('查询失败')
    } else {
    console.log(ret)
    }
    })
    // **********************
    // #endregion /查询数据
    // **********************


    // **********************
    // #region /删除数据
    // **********************
    User.remove({
    username: 'zs'
    }, function (err, ret) {
    if (err) {
    console.log('删除失败')
    } else {
    console.log('删除成功')
    console.log(ret)
    }
    })
    // **********************
    // #endregion /删除数据
    // **********************


    // **********************
    // #region /更新数据
    // **********************
    User.findByIdAndUpdate('5a001b23d219eb00c8581184', {
    password: '123'
    }, function (err, ret) {
    if (err) {
    console.log('更新失败')
    } else {
    console.log('更新成功')
    }
    })
    // **********************
    // #endregion /更新数据
    // **********************

    附:mongoose所有API都支持promise语法
  • 相关阅读:
    mysql 优化20点
    java function
    设计模式 概览
    Linux安装java1.8并配置环境变量
    windows下一次执行多个sql文件
    mybatis查询结果为空时的返回值问题
    Java中Json与String互转
    SSM Service自动注入失败
    本地安装Mysql5.7过程中出现的一系列问题
    解决本地工具无法连接服务器上的mysql的问题
  • 原文地址:https://www.cnblogs.com/chuanzi/p/10539185.html
Copyright © 2011-2022 走看看