博客
关于我
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 精选 60 道面试题(含答案)
    查看>>
    mysql 索引
    查看>>
    MySQL 索引失效的 15 种场景!
    查看>>
    MySQL 索引深入解析及优化策略
    查看>>
    MySQL 索引的面试题总结
    查看>>
    mysql 索引类型以及创建
    查看>>
    MySQL 索引连环问题,你能答对几个?
    查看>>
    Mysql 索引问题集锦
    查看>>
    Mysql 纵表转换为横表
    查看>>
    mysql 编译安装 window篇
    查看>>
    mysql 网络目录_联机目录数据库
    查看>>
    MySQL 聚簇索引&&二级索引&&辅助索引
    查看>>
    Mysql 脏页 脏读 脏数据
    查看>>
    mysql 自增id和UUID做主键性能分析,及最优方案
    查看>>
    Mysql 自定义函数
    查看>>
    mysql 行转列 列转行
    查看>>
    Mysql 表分区
    查看>>
    mysql 表的操作
    查看>>
    mysql 视图,视图更新删除
    查看>>
    MySQL 触发器
    查看>>