nodejs项目的model操作mongo
想想以前学习hibernate的时候,学习各种表和表之间的映射关系等一对多,多对一,多对多,后来到了工作中,勇哥告诉我, 那时在学习的时候,公司中都直接用外键关联。
这里我们学习下,如何在Nodejs代码中操作数据库。
两种操作数据库的方式:看这篇微博
http://www.cnblogs.com/whoamme/p/3467374.html
var mongodb = require('./db'), markdown = require('markdown').markdown; function Post(name, head, title, tags, post) { this.name = name; this.head = head; this.title = title; this.tags = tags; this.post = post; } module.exports = Post;
插入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | //存储一篇文章及其相关信息 Post.prototype.save = function(callback) { var date = new Date(); //存储各种时间格式,方便以后扩展 var time = { date: date, year : date.getFullYear(), month : date.getFullYear() + "-" + (date.getMonth() + 1), day : date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(), minute : date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) } //要存入数据库的文档 var post = { name: this .name, head: this .head, time: time, title: this .title, tags: this .tags, post: this .post, comments: [], reprint_info: {}, pv: 0 }; //打开数据库 mongodb.open(function (err, db) { if (err) { return callback(err); } //读取 posts 集合 db.collection( 'posts' , function (err, collection) { if (err) { mongodb.close(); return callback(err); } //将文档插入 posts 集合 collection.insert(post, { safe: true }, function (err) { mongodb.close(); if (err) { return callback(err); //失败!返回 err } callback( null ); //返回 err 为 null }); }); }); }; |
更新一篇文章:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | //更新一篇文章及其相关信息 Post.update = function(name, day, title, post, callback) { //打开数据库 mongodb.open(function (err, db) { if (err) { return callback(err); } //读取 posts 集合 db.collection( 'posts' , function (err, collection) { if (err) { mongodb.close(); return callback(err); } //更新文章内容 collection.update({ "name" : name, "time.day" : day, "title" : title }, { $ set : {post: post} }, function (err) { mongodb.close(); if (err) { return callback(err); } callback( null ); }); }); }); }; |
删除一篇文章:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | //删除一篇文章 Post.remove = function(name, day, title, callback) { //打开数据库 mongodb.open(function (err, db) { if (err) { return callback(err); } //读取 posts 集合 db.collection( 'posts' , function (err, collection) { if (err) { mongodb.close(); return callback(err); } //查询要删除的文档 collection.findOne({ "name" : name, "time.day" : day, "title" : title }, function (err, doc) { if (err) { mongodb.close(); return callback(err); } //如果有 reprint_from,即该文章是转载来的,先保存下来 reprint_from var reprint_from = "" ; if (doc.reprint_info.reprint_from) { reprint_from = doc.reprint_info.reprint_from; } if (reprint_from != "" ) { //更新原文章所在文档的 reprint_to collection.update({ "name" : reprint_from.name, "time.day" : reprint_from.day, "title" : reprint_from.title }, { $pull: { "reprint_info.reprint_to" : { "name" : name, "day" : day, "title" : title }} }, function (err) { if (err) { mongodb.close(); return callback(err); } }); } //删除转载来的文章所在的文档 collection.remove({ "name" : name, "time.day" : day, "title" : title }, { w: 1 }, function (err) { mongodb.close(); if (err) { return callback(err); } callback( null ); }); }); }); }); }; |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 继承的思维:从思维模式到架构设计的深度解析
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· Cursor预测程序员行业倒计时:CTO应做好50%裁员计划
· 想让你多爱自己一些的开源计时器
· 大模型 Token 究竟是啥:图解大模型Token
· 用99元买的服务器搭一套CI/CD系统
· 当职场成战场:降职、阴谋与一场硬碰硬的抗争