博客
关于我
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 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
    查看>>
    mysql 导入导出大文件
    查看>>
    mysql 将null转代为0
    查看>>
    mysql 常用
    查看>>
    MySQL 常用列类型
    查看>>
    mysql 常用命令
    查看>>
    Mysql 常见ALTER TABLE操作
    查看>>
    MySQL 常见的 9 种优化方法
    查看>>
    MySQL 常见的开放性问题
    查看>>
    Mysql 常见错误
    查看>>
    MYSQL 幻读(Phantom Problem)不可重复读
    查看>>
    mysql 往字段后面加字符串
    查看>>
    mysql 快速自增假数据, 新增假数据,mysql自增假数据
    查看>>
    Mysql 报错 Field 'id' doesn't have a default value
    查看>>
    MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
    查看>>
    Mysql 拼接多个字段作为查询条件查询方法
    查看>>
    mysql 排序id_mysql如何按特定id排序
    查看>>
    Mysql 提示:Communication link failure
    查看>>
    mysql 插入是否成功_PDO mysql:如何知道插入是否成功
    查看>>
    Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
    查看>>