博客
关于我
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_西安11月销售昨日未上架的产品_20161212
    查看>>
    Mysql——深入浅出InnoDB底层原理
    查看>>
    MySQL“被动”性能优化汇总
    查看>>
    MySQL、HBase 和 Elasticsearch:特点与区别详解
    查看>>
    MySQL、Redis高频面试题汇总
    查看>>
    MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
    查看>>
    mysql一个字段为空时使用另一个字段排序
    查看>>
    MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
    查看>>
    MYSQL一直显示正在启动
    查看>>
    MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
    查看>>
    MySQL万字总结!超详细!
    查看>>
    Mysql下载以及安装(新手入门,超详细)
    查看>>
    MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
    查看>>
    MySQL不同字符集及排序规则详解:业务场景下的最佳选
    查看>>
    Mysql不同官方版本对比
    查看>>
    MySQL与Informix数据库中的同义表创建:深入解析与比较
    查看>>
    mysql与mem_细说 MySQL 之 MEM_ROOT
    查看>>
    MySQL与Oracle的数据迁移注意事项,另附转换工具链接
    查看>>
    mysql丢失更新问题
    查看>>
    MySQL两千万数据优化&迁移
    查看>>