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;
插入:
- //存储一篇文章及其相关信息
- 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
- });
- });
- });
- };
更新一篇文章:
- //更新一篇文章及其相关信息
- 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);
- });
- });
- });
- };
删除一篇文章:
- //删除一篇文章
- 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);
- });
- });
- });
- });
- };
nodejs项目的model操作mongo的更多相关文章
- Django项目的ORM操作之--数据模型类创建
在django项目中,其自带了ORM(Object Relation Mapping)对象关系映射框架,我们在django项目下app的models模块下对类进行操作,通过ORM会将我们对类的操作转化 ...
- nodejs 项目的session验证
原文:https://www.codexpedia.com/node-js/a-very-basic-session-auth-in-node-js-with-express-js/ -------- ...
- Django项目的ORM操作之--模型类数据查询
1.查询基本格式及理解: 类名.objects.[查询条件] 例如我们要查询数据库中一张表(bookinfo)的所有数据,sql语句为:select * from bookinfo, 对应模型类的操作 ...
- 09_Android中ContentProvider和Sqllite混合操作,一个项目调用另外一个项目的ContentProvider
1. 编写ContentPrivider提供者的Android应用 清单文件 <?xml version="1.0" encoding="utf-8"? ...
- 搜刮一些开源项目的APP
iOS完整App资源收集 <iOS完整app资源收集> <GitHub 上有哪些完整的 iOS-App 源码值得参考?> <GitHub 上有哪些完整的 iOS-App ...
- 团队项目——编写项目的Spec
团队项目--编写项目的Spec 一.Spec的目标 spec主要用来说明软件的外部功能,和用户的交互情况,主要用来说明软件内部的设计.图片编辑器是与生活息息相关的一个必备软件,随的流行, ...
- 老项目的#iPhone6与iPhone6Plus适配#LaunchImage适配
本文永久地址为 http://www.cnblogs.com/ChenYilong/p/4020384.html,转载请注明出处. Evernote印象笔记链接:https://www.everno ...
- 老项目的#iPhone6与iPhone6Plus适配#Icon适配
本文永久地址为http://www.cnblogs.com/ChenYilong/p/4020373.html ,转载请注明出处. 这是Evernote印象笔记的链接:https://www ...
- 老项目的#iPhone6与iPhone6Plus适配#iOS8无法开启定位问题和#解决方案#
本文永久地址为 http://www.cnblogs.com/ChenYilong/p/4020359.html,转载请注明出处. iOS8的定位和推送的访问都发生了变化, 下面是iOS7和iOS8申 ...
随机推荐
- Floyd求字典序最小的路径
hdu1384 Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- aliyun oss 文件上传 java.net.SocketTimeoutException Read timed out 问题分析及解决
upload ClientException Read timed out com.aliyun.openservices.ClientException: Read timed out ...
- 牛客网多校赛第七场--C Bit Compression【位运算】【暴力】
链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524 ...
- 南京网络赛I-Skr【回文树模板】
19.32% 1000ms 256000K A number is skr, if and only if it's unchanged after being reversed. For examp ...
- 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, ...
- vue - 计算属性、表单输入绑定
计算属性 computed:{} <!DOCTYPE html> <html> <head> <title></title> </he ...
- cookie.setPath()的用法
正常的cookie只能在一个应用中共享,即:一个cookie只能由创建它的应用获得. 可在同一应用服务器内共享cookie的方法:设置cookie.setPath("/"); ( ...
- 如何编写一个python项目
https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001397616003925a ...
- Oracle监控的关键指标
1.监控事例的等待 select event, , , )) "Prev", , , )) "Curr", count(*) "Tot" f ...
- 004-spring cache-声明性的基于XML的缓存
一.概述 如果注释不是选项(不能访问源代码或没有外部代码),可以使用XML进行声明式缓存.因此,不是注释用于缓存的方法,而是从外部指定目标方法和缓存指令(类似于声明式事务管理建议). <!-- ...