博客
关于我
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/

    你可能感兴趣的文章
    nginx+uwsgi+django
    查看>>
    nginx+vsftp搭建图片服务器
    查看>>
    Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
    查看>>
    nginx-vts + prometheus 监控nginx
    查看>>
    nginx: [emerg] getpwnam(“www”) failed 错误处理方法
    查看>>
    nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
    查看>>
    nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
    查看>>
    Nginx、HAProxy、LVS
    查看>>
    Nginx下配置codeigniter框架方法
    查看>>
    Nginx中使用expires指令实现配置浏览器缓存
    查看>>
    nginx中配置root和alias的区别
    查看>>
    nginx主要流程(未完成)
    查看>>
    Nginx之二:nginx.conf简单配置(参数详解)
    查看>>
    Nginx从入门到精通
    查看>>
    Nginx代理websocket配置(解决websocket异常断开连接tcp连接不断问题)
    查看>>
    Nginx代理初探
    查看>>
    nginx代理地图服务--离线部署地图服务(地图数据篇.4)
    查看>>
    Nginx代理外网映射
    查看>>
    Nginx代理模式下 log-format 获取客户端真实IP
    查看>>
    Nginx代理解决跨域问题(导致图片只能预览不能下载)
    查看>>