node查询mongo
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/
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 | 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 31 32 33 34 35 36 37 | 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 }); }); }); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //打开数据库 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方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //打开数据库 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()函数返回对象
1 2 3 4 5 6 7 8 9 10 | //将文档插入 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
1 2 3 4 5 6 7 8 | //distinct 用来找出给定键的所有不同值 collection.distinct( "tags" , function (err, docs) { mongodb.close(); if (err) { return callback(err); } callback( null , docs); }); |
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 | //返回含有特定标签的所有文章 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); }); }); }); }; |
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.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); }); }); }); }; |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步