http://www.cnblogs.com/whoamme/p/3467374.html

nosql的数据库的查询:可以分为查询所有,查询一个,条件查询,和表的关联查询。(这个另外在写一个独立的mongo吧)

看这个api:http://api.mongodb.com/

http://mongodb.github.io/node-mongodb-native/2.0/api/

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.prototype.save = function(callback) {
//要存入数据库的文档
var post = {
name: this.name,
head: this.head,
time: this.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
});
});
});
};

  

//打开数据库
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
});
});
});

  调用db.coolection方法

 //打开数据库
mongodb.open(function (err, db) {
if (err) {
return callback(err);
}
//读取 posts 集合
db.collection('posts', function (err, collection) {
if (err) {
mongodb.close();
return callback(err);
} });
});

利用上面的db.collection()函数返回对象

 //将文档插入 posts 集合
collection.insert(post, {
safe: true
}, function (err) {
mongodb.close();
if (err) {
return callback(err);//失败!返回 err
}
callback(null);//返回 err 为 null
});

  

//一次获取十篇文章  count方法
 

  

var query = {};
if (name) {
query.name = name;
}
//使用 count 返回特定查询的文档数 total
collection.count(query, function (err, total) {
//根据 query 对象查询,并跳过前 (page-1)*10 个结果,返回之后的 10 个结果
collection.find(query, {
skip: (page - 1)*10,
limit: 10
}).sort({
time: -1
}).toArray(function (err, docs) {
mongodb.close();
if (err) {
return callback(err);
}
//解析 markdown 为 html
docs.forEach(function (doc) {
doc.post = markdown.toHTML(doc.post);
});
callback(null, docs, total);
});
});

distinct

//distinct 用来找出给定键的所有不同值
collection.distinct("tags", function (err, docs) {
mongodb.close();
if (err) {
return callback(err);
}
callback(null, docs);
});

  

//返回含有特定标签的所有文章
Post.getTag = function(tag, callback) {
mongodb.open(function (err, db) {
if (err) {
return callback(err);
}
db.collection('posts', function (err, collection) {
if (err) {
mongodb.close();
return callback(err);
}
//查询所有 tags 数组内包含 tag 的文档
//并返回只含有 name、time、title 组成的数组
collection.find({
"tags": tag
}, {
"name": 1,
"time": 1,
"title": 1
}).sort({
time: -1
}).toArray(function (err, docs) {
mongodb.close();
if (err) {
return callback(err);
}
callback(null, docs);
});
});
});
};

  

//返回通过标题关键字查询的所有文章信息
Post.search = function(keyword, callback) {
mongodb.open(function (err, db) {
if (err) {
return callback(err);
}
db.collection('posts', function (err, collection) {
if (err) {
mongodb.close();
return callback(err);
}
var pattern = new RegExp(keyword, "i");
collection.find({
"title": pattern
}, {
"name": 1,
"time": 1,
"title": 1
}).sort({
time: -1
}).toArray(function (err, docs) {
mongodb.close();
if (err) {
return callback(err);
}
callback(null, docs);
});
});
});
};

  

最新文章

  1. Python语法基础
  2. my_strlen()
  3. hdu 2553 N皇后问题
  4. 启动Mysql时发生的一个关于PID文件错误问题
  5. 夺命雷公狗—angularjs—12—get参数的接收
  6. Oracle的导入导出
  7. 反弹SHELL汇总
  8. BSD历史
  9. 应用引擎BAE3.0介绍及百度BAE3.0支持并发多少
  10. Bitmap 与ImageSource之间的转换
  11. 各种文件的ContentType
  12. hibernate使用注解配置索引
  13. 1、Linux命令随笔
  14. UML学习——类之间的关系
  15. nginx支持跨域访问
  16. Hbase记录-备份与恢复方案推荐
  17. DBus send byte array over gdbus ----Send dbus data
  18. 学习CSS布局 - position
  19. hdoj 1753 大明A+B 高精度/java
  20. 从win10家庭版/中文版升级到win10专业版

热门文章

  1. 【开发者笔记】归并排序过程呈现之java内置GUI表示
  2. JavaScript:学习笔记(8)——对象扩展运算符
  3. Centos 6.5 Install Mysql 8.0.0
  4. 论文笔记:dropout
  5. 2 安装企业wiki:confluence
  6. kivy sdl2 - ImportError: DLL load failed: 找不到指定的模块
  7. ROS学习
  8. 20145216史婧瑶《Java程序设计》第2周学习总结
  9. 团队项目系列博客 —— 在路上(之wampserver 修改根目录以及配置多站点以及修改端口号)
  10. ubuntu 12.04及12.10无法安装 ia32-libs