NoSQL是Not Only SQL的缩写,而不是Not SQL,它不一定遵循传统数据库的一些基本要求,如SQL标准、ACID属性、表结构等。
相比传统数据库,叫它分布式数据管理系统更贴切,数据存储被简化更灵活,重点被放在了分布式数据管理上。
关系数据库的表结构
(学生、地址、成绩、科目)
NoSQL的表结构(学生、地址、成绩、科目):
关系数据库处理大数据的弱势
面对高并发读写的需求,数据库读写压力巨大,硬盘IO无法承受。
面对海量数据,数据库能存储的记录数量有限,SQL查询效率极低。
横向扩展艰难,无法通过快速增加服务器节点实现,系统升级和维护造成服务不可用。
NoSQL处理大数据的优势
数据库结构简单,数据间无关系性,这自然就带来了很高的读写性能,且易扩展。
灵活的数据模型,可以随时存储自定义的数据格式。而在关系数据库里,增删字段是件麻烦的事。尤其大数据量的表,增加字段简直就是一个噩梦。
通过复制模型可实现高可用性,可以分布部署在低廉的PC集群上。
支持动态增加、删除服务器节点,随时控制硬件投入成本。
MongoDB是什么?
一个分布式文件存储数据库,由C++语言编写的开源项
一个NoSQL中,功能最丰富、最像关系数据库的产品
它支持的数据结构非常松散,是类似JSON的BSON格式,因此可以存储比较复杂的数据模型
它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,而且还支持索引、MapReduce等功能
MongoDB特点:
高性能、易部署、易使用,存储数据方便;
模式自由,支持动态查询、完全索引、文档内嵌查询;
面向文档,以K/V形式存储数据,Key用于唯一标识,而Value则可以是各种复杂的数据类型;
支持主/从服务器间的数据复制和故障恢复;
自动分片,以支持云级别的服务伸缩性,可动态添加、删除额外的服务器;
支持的数据类型:
布尔:ture |false;
整数:123;
浮点:12.3;
字符串:'helloworld';
对象ID:用newObjectId()来声明;
日期:用newDate()来声明;
时间戳;
数组:['apple','banana', 'pear'];
内嵌文档:{name:'a', phone:{mobile: 123, home: 321}};
RegExp:正则表达式/[a-f]/;
等。
MongoDB适用场景
网站动态数据,需要实时的插入,更新与查询
可以做高性能的持久化缓存层
高伸缩性的集群场景
文档化结构的数据存储及查询
MongoDB非适用场景
数据库事务一致性需求
数据库的写实时性和读实时性需求
对复杂的SQL查询,特别是多表关联查询的需求
Linux:输入命令:npm install mongoose
Ubuntu安装:输入命令:sudo apt-get install mongodb
安装完成可输入命令查看MongoDB版本: mongo –version
Windows安装:
可参考:http://www.runoob.com/mongodb/mongodb-window-install.html
Mongodb及可视化工具:
链接:https://pan.baidu.com/s/1jhsICK0aB6BjYVbMXWooAw密码:n9cj
连接字符串
var mongoose = require('mongoose'),
DB_URL = 'mongodb://localhost:27017/mongoosesample';
/**
* 连接
*/
mongoose.connect(DB_URL);
/**
* 连接成功
*/
mongoose.connection.on('connected',function () {
console.log('Mongoose connection open to ' + DB_URL);
});
/**
* 连接异常
*/
mongoose.connection.on('error',function(err) {
console.log('Mongoose connection error: ' + err);
});
/**
* 连接断开
*/
mongoose.connection.on('disconnected',function () {
console.log('Mongoose connection disconnected');
});
建表
/**
* 用户信息
*/
var mongoose = require('./db.js'),
Schema = mongoose.Schema;
var UserSchema = new Schema({
username : { type: String }, //用户账号
userpwd: {type: String}, //密码
userage: {type: Number}, //年龄
logindate : { type: Date} //最近登录时间
});
module.exports =mongoose.model('User',UserSchema);
API
WHERE
#select * from students where name = 'Joe'
> db.students.find({name : 'Joe'})
FIELDS
#select name, age from students where age = 33
> db.students.find({age : 33}, {name : 1, age : 1})
# select name, age from students
> db.students.find({}, {name : 1, age : 1})
SORT
#select * from students order by age
> db.students.find().sort({age : 1})
# select * from students order by sex asce, age desc
> db.students.find().sort({sex : 1, age : -1})
SLICE
#select * from students skip 2 limit 3
> db.students.find().skip(2).limit(3)
# select * from students where sex = 'male' and age > 18 and age 30
> db.students.find({sex : “male”, age : {$gt: 18, $lt : 30}})
比较操作对比:$gt (>)、$lt (<)、$gte (="">=)、$lte(<=)、$ne (!="">=)、$ne>)、$gte>
举例:插入数据
/**
* 插入
*/
function insert() {
var user = new User({
username : 'Tracy McGrady', //用户账号
userpwd: 'abcd', //密码
userage: 37, //年龄
logindate : new Date() //最近登录时间
});
user.save(function (err, res) {
if (err) {
console.log('Error:' + err);
}
else {
console.log('Res:' + res);
}
});
}
insert();
更多API参考:http://www.runoob.com/nodejs/nodejs-mongodb.html
联系客服