mongo数据库

开启 MongoDB 服务器

1
sudo  mongod --dbpath /usr/local/var/mongodb

连接 MongoDB 服务器

1
mongo

退出 MongoDB 服务器

1
exit

基本命令

  • show dbs

    • 查看显示所有数据库
  • db

    • 产看当前操作的数据库
  • use 数据库名称

    • 切换到指定的数据库(没有会新建)
  • show collections

    • 显示集合
  • db.cats.find()

    • 查看并显示内容
  • node 中使用 mongodb

使用第三方 mongoose 来操作 MongoDB 数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//引入包
const mongoose = require('mongoose');
//连接数据库(数据库不需要存在,在插入第一条语句,就会自动创建)

mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true});

//设计数据库集合结构
//创建一个Cat表
const Cat = mongoose.model('Cat', { name: String });

//实例化一个Cat,实例对象为kitty
const kitty = new Cat({ name: 'Zildjian' });

//持久化保存kitty实例
kitty.save().then(() => console.log('meow'));
  • mongoose 的基本使用
  • 生成模型构造函数
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
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

//设计集合结构(表结构)schema--->提要,纲要
//字段名称就是表结构中的属性名称
//值类型
//约束的目的:为了保证数的完整性
var blogSchema = new Schema({
title: String,
author: String,
body: String,
username: {
type: String,
required: true
},
password: {
typte: String,
required: true
}
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});

//将文档结构发布为模型
// 参数一:大写单数表示数据库名称
// mongoose会自动将大写单数改为小写复数
// 参数二: 架构 Schema
// 返回值: 模型构造函数
var User = mongoose.model('User', blogSchema)
// module.exports = mongoose.model('Student', blogSchema)

基于模型构造函数 生成添加一条数据

1
2
3
4
5
6
7
8
9
10
11
12
var admin = new User({
username: 'admin',
password: '1245435',
....
})
admin.save(function(err, ret){
if(err){
console.log('保存失败')
} else {
console.log('保存成功')
}
})

基于模型构造函数 查询数据

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
//查询所有数据
User.find(function(err, ret){
if(err){
console.log('查询失败')
} else {
console.log(ret)
}
})


//promise 查询所有
User.find()
.then(function(data){
console.log(data)
})


//按条件查询
User.find({
username: 'xx'
}, function(err, ret){
if(err){
console.log('查询失败')
} else {
console.log(ret)
}
})
//查找符合条件的第一个(可以写多个条件),没有条件的话,为第一条数据
User.findOne({
username: 'xx'
}, function(err, ret){
if(err){
console.log('查询失败')
} else {
console.log(ret)
}
})

基于模型构造函数 按条件更新数据

1
2
3
4
5
6
7
8
9
10
11
//Model.update(conditions, doc, [options], [callback])
//Model.findOneAndUpdate([conditions], [update], [options], [callback])
User.findByIdAndUpdate('5a7d8f2sd78ag6g',{
username: 'zs'
}, function(err, ret){
if(err){
console.log('更新失败')
else {
console.log('更新成功')
}
})

基于模型构造函数 删除数据

1
2
3
4
5
6
7
8
9
User.remove({
username: 'zs'
}, function(err, ret){
if(err){
console.log('查询失败')
else {
console.log(ret)
}
})

补充(node 使用 mysql)

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
//引包
var mysql = require('mysql');
//创建连接
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});

//连接数据库
connection.connect();

//执行数据操作,可以直接使用 SQL 语句
//查找
connection.query('SELECT * FROM `users`', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});

//添加
connection.query('INSERT INTO users VALUES(NULL, "admin":"123")', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
//关闭数据库
connection.end();

阿里云服务器

  1. 安装 nodejs
  2. 安装 mongod
  3. 安装 redis
    3.1 redis-server 启动 redis
    3.2 ps axu | grep redis 查看 redis 服务进程
  4. 安装 nginx
    4.1 nginx -s reopen // 重新指定日志打印的文件
    4.2 nginx - t //检测配置文件是否有语法错误
    4.3 nginx -s reload // 重新加载配置文件

评论