想想以前学习hibernate的时候,学习各种表和表之间的映射关系等一对多,多对一,多对多,后来到了工作中,勇哥告诉我, 那时在学习的时候,公司中都直接用外键关联。

这里我们学习下,如何在Nodejs代码中操作数据库。

两种操作数据库的方式:看这篇微博
http://www.cnblogs.com/whoamme/p/3467374.html

  1. var mongodb = require('./db'),
  2. markdown = require('markdown').markdown;
  3.  
  4. function Post(name, head, title, tags, post) {
  5. this.name = name;
  6. this.head = head;
  7. this.title = title;
  8. this.tags = tags;
  9. this.post = post;
  10. }
  11.  
  12. module.exports = Post;

插入:

  1. //存储一篇文章及其相关信息
  2. Post.prototype.save = function(callback) {
  3. var date = new Date();
  4. //存储各种时间格式,方便以后扩展
  5. var time = {
  6. date: date,
  7. year : date.getFullYear(),
  8. month : date.getFullYear() + "-" + (date.getMonth() + 1),
  9. day : date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
  10. minute : date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " +
  11. date.getHours() + ":" + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
  12. }
  13. //要存入数据库的文档
  14. var post = {
  15. name: this.name,
  16. head: this.head,
  17. time: time,
  18. title:this.title,
  19. tags: this.tags,
  20. post: this.post,
  21. comments: [],
  22. reprint_info: {},
  23. pv: 0
  24. };
  25. //打开数据库
  26. mongodb.open(function (err, db) {
  27. if (err) {
  28. return callback(err);
  29. }
  30. //读取 posts 集合
  31. db.collection('posts', function (err, collection) {
  32. if (err) {
  33. mongodb.close();
  34. return callback(err);
  35. }
  36. //将文档插入 posts 集合
  37. collection.insert(post, {
  38. safe: true
  39. }, function (err) {
  40. mongodb.close();
  41. if (err) {
  42. return callback(err);//失败!返回 err
  43. }
  44. callback(null);//返回 err 为 null
  45. });
  46. });
  47. });
  48. };

  

更新一篇文章:

  1. //更新一篇文章及其相关信息
  2. Post.update = function(name, day, title, post, callback) {
  3. //打开数据库
  4. mongodb.open(function (err, db) {
  5. if (err) {
  6. return callback(err);
  7. }
  8. //读取 posts 集合
  9. db.collection('posts', function (err, collection) {
  10. if (err) {
  11. mongodb.close();
  12. return callback(err);
  13. }
  14. //更新文章内容
  15. collection.update({
  16. "name": name,
  17. "time.day": day,
  18. "title": title
  19. }, {
  20. $set: {post: post}
  21. }, function (err) {
  22. mongodb.close();
  23. if (err) {
  24. return callback(err);
  25. }
  26. callback(null);
  27. });
  28. });
  29. });
  30. };

 删除一篇文章:

  1. //删除一篇文章
  2. Post.remove = function(name, day, title, callback) {
  3. //打开数据库
  4. mongodb.open(function (err, db) {
  5. if (err) {
  6. return callback(err);
  7. }
  8. //读取 posts 集合
  9. db.collection('posts', function (err, collection) {
  10. if (err) {
  11. mongodb.close();
  12. return callback(err);
  13. }
  14. //查询要删除的文档
  15. collection.findOne({
  16. "name": name,
  17. "time.day": day,
  18. "title": title
  19. }, function (err, doc) {
  20. if (err) {
  21. mongodb.close();
  22. return callback(err);
  23. }
  24. //如果有 reprint_from,即该文章是转载来的,先保存下来 reprint_from
  25. var reprint_from = "";
  26. if (doc.reprint_info.reprint_from) {
  27. reprint_from = doc.reprint_info.reprint_from;
  28. }
  29. if (reprint_from != "") {
  30. //更新原文章所在文档的 reprint_to
  31. collection.update({
  32. "name": reprint_from.name,
  33. "time.day": reprint_from.day,
  34. "title": reprint_from.title
  35. }, {
  36. $pull: {
  37. "reprint_info.reprint_to": {
  38. "name": name,
  39. "day": day,
  40. "title": title
  41. }}
  42. }, function (err) {
  43. if (err) {
  44. mongodb.close();
  45. return callback(err);
  46. }
  47. });
  48. }
  49.  
  50. //删除转载来的文章所在的文档
  51. collection.remove({
  52. "name": name,
  53. "time.day": day,
  54. "title": title
  55. }, {
  56. w: 1
  57. }, function (err) {
  58. mongodb.close();
  59. if (err) {
  60. return callback(err);
  61. }
  62. callback(null);
  63. });
  64. });
  65. });
  66. });
  67. };

nodejs项目的model操作mongo的更多相关文章

  1. Django项目的ORM操作之--数据模型类创建

    在django项目中,其自带了ORM(Object Relation Mapping)对象关系映射框架,我们在django项目下app的models模块下对类进行操作,通过ORM会将我们对类的操作转化 ...

  2. nodejs 项目的session验证

    原文:https://www.codexpedia.com/node-js/a-very-basic-session-auth-in-node-js-with-express-js/ -------- ...

  3. Django项目的ORM操作之--模型类数据查询

    1.查询基本格式及理解: 类名.objects.[查询条件] 例如我们要查询数据库中一张表(bookinfo)的所有数据,sql语句为:select * from bookinfo, 对应模型类的操作 ...

  4. 09_Android中ContentProvider和Sqllite混合操作,一个项目调用另外一个项目的ContentProvider

    1.  编写ContentPrivider提供者的Android应用 清单文件 <?xml version="1.0" encoding="utf-8"? ...

  5. 搜刮一些开源项目的APP

    iOS完整App资源收集 <iOS完整app资源收集>  <GitHub 上有哪些完整的 iOS-App 源码值得参考?> <GitHub 上有哪些完整的 iOS-App ...

  6. 团队项目——编写项目的Spec

    团队项目--编写项目的Spec 一.Spec的目标        spec主要用来说明软件的外部功能,和用户的交互情况,主要用来说明软件内部的设计.图片编辑器是与生活息息相关的一个必备软件,随的流行, ...

  7. 老项目的#iPhone6与iPhone6Plus适配#LaunchImage适配

    本文永久地址为 http://www.cnblogs.com/ChenYilong/p/4020384.html,转载请注明出处.  Evernote印象笔记链接:https://www.everno ...

  8. 老项目的#iPhone6与iPhone6Plus适配#Icon适配

        本文永久地址为http://www.cnblogs.com/ChenYilong/p/4020373.html ,转载请注明出处.  这是Evernote印象笔记的链接:https://www ...

  9. 老项目的#iPhone6与iPhone6Plus适配#iOS8无法开启定位问题和#解决方案#

    本文永久地址为 http://www.cnblogs.com/ChenYilong/p/4020359.html,转载请注明出处. iOS8的定位和推送的访问都发生了变化, 下面是iOS7和iOS8申 ...

随机推荐

  1. Floyd求字典序最小的路径

    hdu1384 Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  2. aliyun oss 文件上传 java.net.SocketTimeoutException Read timed out 问题分析及解决

    upload ClientException Read timed out com.aliyun.openservices.ClientException: Read timed out        ...

  3. 牛客网多校赛第七场--C Bit Compression【位运算】【暴力】

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524 ...

  4. 南京网络赛I-Skr【回文树模板】

    19.32% 1000ms 256000K A number is skr, if and only if it's unchanged after being reversed. For examp ...

  5. https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic net::ERR_CONNECTION_TIMED_OUT问题解决

    使用adminTLE时,有时候出现 https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic, ...

  6. vue - 计算属性、表单输入绑定

    计算属性 computed:{} <!DOCTYPE html> <html> <head> <title></title> </he ...

  7. cookie.setPath()的用法

    正常的cookie只能在一个应用中共享,即:一个cookie只能由创建它的应用获得. 可在同一应用服务器内共享cookie的方法:设置cookie.setPath("/");  ( ...

  8. 如何编写一个python项目

    https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001397616003925a ...

  9. Oracle监控的关键指标

    1.监控事例的等待 select event, , , )) "Prev", , , )) "Curr", count(*) "Tot" f ...

  10. 004-spring cache-声明性的基于XML的缓存

    一.概述 如果注释不是选项(不能访问源代码或没有外部代码),可以使用XML进行声明式缓存.因此,不是注释用于缓存的方法,而是从外部指定目标方法和缓存指令(类似于声明式事务管理建议). <!-- ...