博客
关于我
3.2 mongoose 增删改查-案例2
阅读量:724 次
发布时间:2019-03-21

本文共 2560 字,大约阅读时间需要 8 分钟。

mongoose 操作(增删改查)案例详解

1. 用户管理表操作

1.1 查询数据

使用mongoose查询用户数据:

const mongoose = require('mongoose');mongoose.connect('mongodb://localhost/dadi', { useNewUrlParser: true, useUnifiedTopology: true });const UserSchema = mongoose.Schema({    name: String,    age: Number,    status: Number});const User = mongoose.model('User', UserSchema);// 查询所有用户数据User.find({}, function(err, docs) {    if (err) {        console.log('查询失败:', err);    } else {        console.log('查询成功:', docs);    }});

1.2 增加用户数据

新增用户信息:

const user = new User({    name: '张三',    age: 30,    status: 1});user.save((err, result) => {    if (err) {        console.error('用户数据存储失败:', err);    } else {        console.log('用户数据已成功存储:', result);    }});

1.3 更新用户信息

更新特定用户信息:

User.updateOne({ _id: '5f3c002c-aPARTa-XXXX-XXXX-XXXX-XXXXX' }, {    name: '张三 updated',    age: 35}, (err, result) => {    if (err) {        console.error('更新失败:', err);    } else {        console.log('用户信息更新成功:', result);    }});

1.4 删除用户信息

删除特定用户:

User.deleteOne({ _id: '5f3c002c-aPARTa-XXXX-XXXX-XXXX-XXXXX' }, function(err, result) {    if (err) {        console.error('删除失败:', err);    } else {        console.log('用户数据已删除:', result);    }});

2. 新闻管理表操作

2.1 创建新闻表

首次操作新闻表(需要手动创建集合):

const mongoose = require('mongoose');mongoose.connect('mongodb://localhost/dadi', { useNewUrlParser: true, useUnifiedTopology: true });const NewsSchema = mongoose.Schema({    title: String,    author: String,    pic: String,    status: Number});const News = mongoose.model('News', NewsSchema, 'news'); // 第三个参数指定集合名

2.2 新增新闻数据

新增一条新闻:

const news = new News({    title: '第一条新闻',    author: '管理员',    pic: 'news1.jpg',    status: 1});news.save((err, result) => {    if (err) {        console.error('新闻数据存储失败:', err);    } else {        console.log('新闻数据已成功存储:', result);    }});

2.3 查询新闻数据

查询所有新闻:

News.find({}, (err, newsList) => {    if (err) {        console.log('查询失败:', err);    } else {        console.log('新闻列表:', newsList);    }});

2.4 更新新闻内容

更新特定新闻:

News.updateOne({ _id: '5f3c002c-aPARTb-XXXX-XXXX-XXXX-XXXXX' }, {    title: '重要消息更新',    author: '管理员 updated'}, (err, result) => {    if (err) {        console.error('更新失败:', err);    } else {        console.log('新闻信息更新成功:', result);    }});

2.5 删除新闻数据

删除特定新闻:

News.deleteOne({ _id: '5f3c002c-aPARTb-XXXX-XXXX-XXXX-XXXXX' }, (err, result) => {    if (err) {        console.error('删除失败:', err);    } else {        console.log('新闻数据已删除:', result);    }});

注意事项

  • 确保数据库服务器正常运行,并且权限设置正确。
  • 运行以上实验前,请自行创建相应的数据库和集合,避免操作异常。
  • 对于大数据量操作,建议提升数据库性能,并根据实际需求优化查询方式。
  • 注意在开发过程中遵守相关数据库管理规范,确保数据安全和系统稳定。
  • 转载地址:http://jqbgz.baihongyu.com/

    你可能感兴趣的文章
    mysql-开启慢查询&所有操作记录日志
    查看>>
    MySQL-数据目录
    查看>>
    MySQL-数据页的结构
    查看>>
    MySQL-架构篇
    查看>>
    MySQL-索引的分类(聚簇索引、二级索引、联合索引)
    查看>>
    Mysql-触发器及创建触发器失败原因
    查看>>
    MySQL-连接
    查看>>
    mysql-递归查询(二)
    查看>>
    MySQL5.1安装
    查看>>
    mysql5.5和5.6版本间的坑
    查看>>
    mysql5.5最简安装教程
    查看>>
    mysql5.6 TIME,DATETIME,TIMESTAMP
    查看>>
    mysql5.6.21重置数据库的root密码
    查看>>
    Mysql5.6主从复制-基于binlog
    查看>>
    MySQL5.6忘记root密码(win平台)
    查看>>
    MySQL5.6的Linux安装shell脚本之二进制安装(一)
    查看>>
    MySQL5.6的zip包安装教程
    查看>>
    mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
    查看>>
    Webpack 基本环境搭建
    查看>>
    mysql5.7 安装版 表不能输入汉字解决方案
    查看>>