上午一:
Mac 环境准备:
1.安装 cnpm, 打开终端,在命令行输入命令
sudo npm install -g cnpm --registry=https://registry.npm.taobao.org
2.安装 homebrew, 在命令行中输入命令
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
3.安装 MongoDB, 在命令行中输入命令
brew install mongodb
数据持久化
数据永久保存
数据持久化的方式:
1.文件读写的形式
2.cookie, 服务器放在客户端的数据; 客户端会自动的将 cookie 发送给服务器(cookie 存放在 http Header 中), session + cookie
3.localStorage, 客户端本地存储
4.数据库存储
mongodb 属于非关系型数据库
关系型数据库: 如 mysql, 表和表之间可以建立关联(主键,外键,索引),依赖,对关系型数据库进行操作,一般使用 sql(结构化查询语句)
非关系型数据库: 如 mongodb, 支持大数据的处理, 集群化(多台服务器连接在一起), 分布式(服务器所在的位置不一样)
关系型 非关系型
数据库 数据库
表 集合
字段 域
mongodb 使用
1.进入 mongodb 环境
命令 mongo
注: 默认会连接 test 数据库, 如果连接不上, 可以自己创建一个数据库
2.新建 mongodb 的数据库
命令: mongod --dbpath 文件夹路径
3.在 mongo 环境下(出现>), 显示所有的数据库
命令: show dbs
4.切换数据库(如果该数据不存在,并且对该数据库进行操作,则会新建该数据库)
命令: use 数据库名
5.新建集合
命令: db.createCollection('集合名')
基本操作
CRUD 操作: 增(Create)查(Retrieve)改(Update)删(Delete)
6.查询
命令: db.集合名.find()
7.插入
命令: db.集合名.insert(数据)
添加一条数据
例: db.student.insert({name:'张三',gender:'男',age:21})
添加多条数据
例: db.student.insert([{name:'小明',gender:'男',age:15},{name:'小红',gender:'女',age:13}])
注: 插入成功后会自动给该条数据添加主键 _id
注: 插入的集合不存在时,会新建该集合并插入数据
数据的格式为 BSON
BSON: Binary JSON, 一种类 JSON 的二进制数据, 和 JSON 结构一样({}:文档对象[]:数组对象), 比 JSON 支持的数据类型
要多(如 Data, BinData)
8.显示数据库中的集合
命令: show Collections
9.删除数据库中的集合
命令: db.集合名.drop()
10.修改数据
命令: db.集合名.update(查询条件, 修改的内容, 可选参数)
修改某条数据
例: db.student.update({name:'张三'},{$set:{name:'张三疯'}})
注:如果集合中存在多条名为张三的数据,以上命令只修改第一次出现的那条数据
修改多条
例: db.student.update({name:'张三'},{$set:{name:'张三疯'}},{multi:true})
11.删除数据
命令: db.集合名.remove(查询条件, 可选参数)
删除多条
例: db.student.remove({name:'张三疯'})
删除一条
例: db.student.remove({name:'张三疯'},{justOne:true})
mongodb 的可视化操作软件: robomongo
上午二:
在 nodejs 中使用 mongoose 模块
安装命令: cnpm install mongoose
//导入模块
var mongoose = require('mongoose');
//连接数据库
var db = mongoose.connect("mongodb://127.0.0.1:27017/yettoo");
//监听数据库是否打开
db.connection.on("open", function(){
console.log("数据库连接成功");
//mongoose 数据库操作,基于数据模型进行
//要求设置数据模型结构,用于规范操作数据的结构
var scheme = new mongoose.Schema({
name: {type: String},
gender: {type: String},
age: {type: Number}
},{
collection: "student"
});
var model = db.model("student", scheme);
//查询
model.find({}, function(error, result){
if (error) {
console.error(error);
return;
}
//这儿的 result 返回一个数组对象
console.log(result);
});
model.findOne({}, function(error, result){
if (error) {
console.error(error);
return;
}
//这儿的 result返回对象
console.log(result);
});
//通过 id 进行查询
model.findById("57fc5a8b4e418a4ad294fec3", function(error, result){
console.log(result);
});
//条件查询
//返回一个数组
//$gt 大于, $lt 小于, $ne 不等于
//1.大于
model.find({age:{$gt:18}}, function (error, result) {
console.log("年龄大于18岁的人:");
console.log(result);
});
//2.小于
model.find({age:{$lt:18}}, function(error, result){
console.log("年龄小于18岁的有:");
console.log(result);
});
//3.等于
model.find({age:18}, function(error, result){
console.log("年龄为18岁的有:");
console.log(result);
});
//4.不等于
model.find({age:{$ne:18}}, function(error, result){
console.log("年龄不为18岁的有:");
console.log(result);
});
//5.大于等于
//gte, greater than equal
model.find({age:{$gte:18}}, function(error, result){
console.log("年龄大于等于18岁的有:");
console.log(result);
});
//6.小于等于
model.find({age:{$lte:18}}, function(error, result){
console.log("年龄小于等于18岁的有:");
console.log(result);
});
//7.多条件查询
model.find({$or:[{name:'小明'},{gender:'女'}]}, function(error, result){
console.log("名字叫小明或者女生的有:");
console.log(result);
});
//8.判断是否包含某个字段
model.find({a:{$exists:true}}, function(error, result){
console.log(" 包含该字段的数据:");
console.log(result);
});
//9.限制查询的数量
model.find({},null,{limit:3}, function(error, result){
console.log("前三条数据为:");
console.log(result);
});
//10.跳过某几条数据
model.find({},null,{skip:2}, function(error, result){
console.log("跳过前两条数据:");
console.log(result);
});
//11.排序
model.find({},null,{sort:{age:1}}, function(error, result){
console.log("按照年龄进行升序排序:");
console.log(result);
});
model.find({},null,{sort:{age:-1}}, function(error, result){
console.log("按照年龄进行降序排序");
console.log(result);
});
//插入数据
model.create({
name: "光头强",
gender: "男",
age: 35
}, function(error, result){
console.log("插入数据:" + result);
});
//实体:用模型生成
var entity = new model({
name: "熊二",
gender: "熊",
age: 3
});
//保存实体
entity.save(function(error, result){
console.log("光头强又在砍树了"+result);
});
});
db.connection.on("error", function(){
console.error("数据库连接失败" + error);
});
下午:
angular.js
是一个动态的 web 应用框架, 可以让你扩展 html 语法,用于增强 html 的功能
官网: https://angularjs.org
版本选择:
最新版: 2.x
稳定版: 1.5.x
兼容 IE8: 1.2.x
Mac 环境准备:
1.安装 cnpm, 打开终端,在命令行输入命令
sudo npm install -g cnpm --registry=https://registry.npm.taobao.org
2.安装 homebrew, 在命令行中输入命令
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
3.安装 MongoDB, 在命令行中输入命令
brew install mongodb
数据持久化
数据永久保存
数据持久化的方式:
1.文件读写的形式
2.cookie, 服务器放在客户端的数据; 客户端会自动的将 cookie 发送给服务器(cookie 存放在 http Header 中), session + cookie
3.localStorage, 客户端本地存储
4.数据库存储
mongodb 属于非关系型数据库
关系型数据库: 如 mysql, 表和表之间可以建立关联(主键,外键,索引),依赖,对关系型数据库进行操作,一般使用 sql(结构化查询语句)
非关系型数据库: 如 mongodb, 支持大数据的处理, 集群化(多台服务器连接在一起), 分布式(服务器所在的位置不一样)
关系型 非关系型
数据库 数据库
表 集合
字段 域
mongodb 使用
1.进入 mongodb 环境
命令 mongo
注: 默认会连接 test 数据库, 如果连接不上, 可以自己创建一个数据库
2.新建 mongodb 的数据库
命令: mongod --dbpath 文件夹路径
3.在 mongo 环境下(出现>), 显示所有的数据库
命令: show dbs
4.切换数据库(如果该数据不存在,并且对该数据库进行操作,则会新建该数据库)
命令: use 数据库名
5.新建集合
命令: db.createCollection('集合名')
基本操作
CRUD 操作: 增(Create)查(Retrieve)改(Update)删(Delete)
6.查询
命令: db.集合名.find()
7.插入
命令: db.集合名.insert(数据)
添加一条数据
例: db.student.insert({name:'张三',gender:'男',age:21})
添加多条数据
例: db.student.insert([{name:'小明',gender:'男',age:15},{name:'小红',gender:'女',age:13}])
注: 插入成功后会自动给该条数据添加主键 _id
注: 插入的集合不存在时,会新建该集合并插入数据
数据的格式为 BSON
BSON: Binary JSON, 一种类 JSON 的二进制数据, 和 JSON 结构一样({}:文档对象[]:数组对象), 比 JSON 支持的数据类型
要多(如 Data, BinData)
8.显示数据库中的集合
命令: show Collections
9.删除数据库中的集合
命令: db.集合名.drop()
10.修改数据
命令: db.集合名.update(查询条件, 修改的内容, 可选参数)
修改某条数据
例: db.student.update({name:'张三'},{$set:{name:'张三疯'}})
注:如果集合中存在多条名为张三的数据,以上命令只修改第一次出现的那条数据
修改多条
例: db.student.update({name:'张三'},{$set:{name:'张三疯'}},{multi:true})
11.删除数据
命令: db.集合名.remove(查询条件, 可选参数)
删除多条
例: db.student.remove({name:'张三疯'})
删除一条
例: db.student.remove({name:'张三疯'},{justOne:true})
mongodb 的可视化操作软件: robomongo
上午二:
在 nodejs 中使用 mongoose 模块
安装命令: cnpm install mongoose
//导入模块
var mongoose = require('mongoose');
//连接数据库
var db = mongoose.connect("mongodb://127.0.0.1:27017/yettoo");
//监听数据库是否打开
db.connection.on("open", function(){
console.log("数据库连接成功");
//mongoose 数据库操作,基于数据模型进行
//要求设置数据模型结构,用于规范操作数据的结构
var scheme = new mongoose.Schema({
name: {type: String},
gender: {type: String},
age: {type: Number}
},{
collection: "student"
});
var model = db.model("student", scheme);
//查询
model.find({}, function(error, result){
if (error) {
console.error(error);
return;
}
//这儿的 result 返回一个数组对象
console.log(result);
});
model.findOne({}, function(error, result){
if (error) {
console.error(error);
return;
}
//这儿的 result返回对象
console.log(result);
});
//通过 id 进行查询
model.findById("57fc5a8b4e418a4ad294fec3", function(error, result){
console.log(result);
});
//条件查询
//返回一个数组
//$gt 大于, $lt 小于, $ne 不等于
//1.大于
model.find({age:{$gt:18}}, function (error, result) {
console.log("年龄大于18岁的人:");
console.log(result);
});
//2.小于
model.find({age:{$lt:18}}, function(error, result){
console.log("年龄小于18岁的有:");
console.log(result);
});
//3.等于
model.find({age:18}, function(error, result){
console.log("年龄为18岁的有:");
console.log(result);
});
//4.不等于
model.find({age:{$ne:18}}, function(error, result){
console.log("年龄不为18岁的有:");
console.log(result);
});
//5.大于等于
//gte, greater than equal
model.find({age:{$gte:18}}, function(error, result){
console.log("年龄大于等于18岁的有:");
console.log(result);
});
//6.小于等于
model.find({age:{$lte:18}}, function(error, result){
console.log("年龄小于等于18岁的有:");
console.log(result);
});
//7.多条件查询
model.find({$or:[{name:'小明'},{gender:'女'}]}, function(error, result){
console.log("名字叫小明或者女生的有:");
console.log(result);
});
//8.判断是否包含某个字段
model.find({a:{$exists:true}}, function(error, result){
console.log(" 包含该字段的数据:");
console.log(result);
});
//9.限制查询的数量
model.find({},null,{limit:3}, function(error, result){
console.log("前三条数据为:");
console.log(result);
});
//10.跳过某几条数据
model.find({},null,{skip:2}, function(error, result){
console.log("跳过前两条数据:");
console.log(result);
});
//11.排序
model.find({},null,{sort:{age:1}}, function(error, result){
console.log("按照年龄进行升序排序:");
console.log(result);
});
model.find({},null,{sort:{age:-1}}, function(error, result){
console.log("按照年龄进行降序排序");
console.log(result);
});
//插入数据
model.create({
name: "光头强",
gender: "男",
age: 35
}, function(error, result){
console.log("插入数据:" + result);
});
//实体:用模型生成
var entity = new model({
name: "熊二",
gender: "熊",
age: 3
});
//保存实体
entity.save(function(error, result){
console.log("光头强又在砍树了"+result);
});
});
db.connection.on("error", function(){
console.error("数据库连接失败" + error);
});
下午:
angular.js
是一个动态的 web 应用框架, 可以让你扩展 html 语法,用于增强 html 的功能
官网: https://angularjs.org
版本选择:
最新版: 2.x
稳定版: 1.5.x
兼容 IE8: 1.2.x