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);
});
});
});
};

  

node查询mongo的更多相关文章

  1. 使用node查询数据库(mysql)时,日期格式不对的问题。

    https://blog.csdn.net/chanlingmai5374/article/details/93190983 1.问题场景 数据库里存了 datetime.但 Node 查询出来是这样 ...

  2. node + nginx + mongo搭建负载均衡

    基于node和nignx和mongo搭建负载均衡 nginx配置: upstream back {                                                  # ...

  3. node、Mongo项目如何前后端分离提供接口给前端

    node接口编写,vue-cli代理接口方法  通常前端使用的MocK 数据的方法,去模拟假的数据,但是如果有node Mongodb 去写数据的话就不需要在去mock 数据了,具体的方法如下. 首先 ...

  4. 夺命雷公狗---node.js---22之项目的构建在node+express+mongo的博客项目7之数据的修改

    在修改的时候和在PHP里面修改的一样,都是需要在列表页传个id过来才可以实现修改的,如下所示: 然后在后端接收到他传过来id的同时去读取该id的所有信息: 然后就开始写post处理数据方面的问题了,如 ...

  5. 夺命雷公狗---node.js---21之项目的构建在node+express+mongo的博客项目6之数据的遍历

    首先还是来链接数据库,然后就查找,如下所示: /** * Created by leigood on 2016/8/31. */ var express = require('express'); v ...

  6. 夺命雷公狗---node.js---20之项目的构建在node+express+mongo的博客项目5mongodb在项目中实现添加数据

    我们上一步就引入了mongodb了,那么下一步就要开始写添加数据了,不过有个前提是先将表单的数据处理好: 最基本的这部现在已经成功了,因为最基本的这步就是先将表单处的提交方式和提交地址给处理好,这里和 ...

  7. 夺命雷公狗---node.js---19之项目的构建在node+express+mongo的博客项目4mongodb在项目中的基本引入

    首先我们在命令行下先建立这个库: 然后我们在项目中引入mongodb的模块: var MongoClient = require('mongodb').MongoClient; var DB_STR ...

  8. 夺命雷公狗---node.js---18之项目的构建在node+express+mongo的博客项目3头尾左侧分离法

    在实际的开发中我们的项目往往都是需要头尾分离开来的,居然是后台管理界面当然也不能错过这么好的这步.. 首先我们将我们要分离的部分代码先剪切出来,如下所示: 将他们都弄出来... 这部分的内容分别对应的 ...

  9. 夺命雷公狗---node.js---17之项目的构建在node+express+mongo的博客项目2之一,二级路由

    然后我们就来开始搭建后台了... 不过后台我们可以来玩玩他的二级路由... 然后再去修改下他们的样式即可......修改方法和刚才那里的修改方法一样, 访问效果如下所示: OK,已经正常相识了

随机推荐

  1. Linux cal命令

    cal命令时查看日历的相关命令 1.用法 cal [选项] [[[日] 月] 年] 2.命令选项 -1, --one 只显示当前月份(默认) -3, --three  显示上个月.当月和下个月 -s, ...

  2. spring MVC 学习(四)---拦截器,视图解析器

    1.接口HandlerInterceptor 该接口包含3个方法,分别是preHandle,postHandle,afterCompletion,分别代表着执行前,执行后,执行完成要执行的方法,其中p ...

  3. 『HTML5挑战经典』是英雄就下100层-开源讲座(二)危险!英雄

    本篇为<『HTML5挑战经典』是英雄就下100层-开源讲座>第二篇,需要用到开源引擎lufylegend,可以到这里下载: 下载地址:http://lufylegend.googlecod ...

  4. HDU1003:Max Sum(简单dp)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意一目了然就不说了,算法是从左往右扫,一个暂时保存结果的值,如果区间结果<0,那么就更改左右 ...

  5. R中的一些基础1106

    1.R中NA,NaN,Inf代表什么? NA:缺失数据 NaN:无意义的数,比如sqrt(-2) Inf:正无穷大 -Inf:负无穷大 2.确定一个数值型vector的第一个最值(最大/最小)的下标: ...

  6. oracle 数据库误删数据,误删表的恢复

    1.某表的数据误删了,那么可以查询这个表某一时间节点之前的数据,并放到一个新建的表里. create table temptable as select * from t_billdefi  as O ...

  7. Spring MVC 复习笔记01

    1. springmvc框架 1.1 什么是springmvc spring mvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合.spring mvc是一个 ...

  8. Wex5各组件介绍

    1.http://doc.wex5.com/comp-base/ 2.select 组件 http://doc.wex5.com/comps-select/ 3.页面交互以及传递参数  http:// ...

  9. 在python3下使用OpenCV做离散余弦变换DCT及其反变换IDCT

    对图像处理经常用到DCT, Python下有很多带有DCT算法包, 这里使用OpenCV的DCT做变换, 并简单置0部分数据, 再查看反变换图像的效果. import numpy as np impo ...

  10. JPA、JTA与JMS

    三者都属于Java企业级规范 JPA(java persistence API) JPA 通过JDK5.0的注解或XML来描述 对象-关系表的映射关系,并将运行期的实体对象持久化存储到数据库中. JT ...