首页 技术 正文
技术 2022年11月15日
0 收藏 509 点赞 4,870 浏览 2033 个字

最近在研究Nodejs 自然就接触到了MongoDB  这玩意儿有意思  与关系型数据库相比少了很多条条框框 让我情不自禁的想要了解它的所有

MongoDB与Redis同类 属于NoSql的一种,特点是简单,方便扩展,性能更佳。

以下是MongoDB与MySQL的不同:

1.MongoDB事务稍弱(不支持多行多文档多语句原子性更新)
2.MongoDB不支持多表联查。
3.MongoDB没有表结构概念,每条记录可以存入完全不同的数据结构。
4.MongoDB完全的索引支持,这点比Redis纯键值对要强大,单键索引,多键索引,数组索引,全文索引,地图索引。
5.MongoDB速度快 性能优越 (默认操作是写日志 写内存 返回结果 然后才会在后台进行每隔几十毫秒的日志刷盘)
6.MongoDB的数据存储更加安全(默认支持三节点以上的复制集群)

(当然 肯定还有很多是我不知道的)

下载地址
https://www.mongodb.com/download-center#community

下载之后自行移动解压

本篇教程的解压后目录为 /usr/local/mongodb

手动创建配置文件

vim /usr/local/mongodb/mongodb.conf

配置文件写入以下内容:

#数据库存放目录
dbpath=/usr/local/mongodb/data
#是否启动日志
journal=true
#日志文件路径 非目录
logpath=/usr/local/mongodb/run.log
logappend=true
#绑定ip
bind_ip = 127.0.0.1
#绑定端口
port = 27018
#后台启动
fork = true

创建data目录与log文件
mkdir /usr/local/mongodb/data
touch /usr/local/mongodb/run.log

给个权限
chmod 755 -R /usr/local/mongodb

进入bin目录

cd  /usr/local/mongodb/bin/

启动
./mongod -f /usr/local/mongodb/mongodb.conf

出现以下提示即为启动成功:
about to fork child process, waiting until server is ready for connections.
forked process: 16131
child process started successfully, parent exiting

连接数据库
./mongo
默认连接端口为27017 如果配置文件中指定了其他端口 则需要带上 如 ./mongo -port 27018

基本操作:

//查询所有库(空库不会显示)
show dbs
//查询所有表
db.getCollectionNames()
//切换数据库(如库不存在会自动创建)
use dbname
//往a表插入内容(如表不存在会自动创建)(MongoDB中的表 被称作为集合)
db.a.insert({username:'admin',password:123456})
//将a表中用户为admin的密码改为111111
db.a.update({'username':'admin'},{$set:{'password':111111}})
//删除a表中用户为admin的数据
db.a.remove({'username':'admin'})

查 与 MySQL对照

//所有的查询后面都可以跟上.pretty()代表格式化输出 如 db.a.find().pretty()db.a.find() select * from adb.a.count() select count(*) from adb.a.findOne()  select * from a limit 1db.a.find({'username':'admin','password':123456}) select * from a where username='admin' and password=123456db.a.find({'username':'admin'}, {password: 1})  select password from a where username='admin'  //1为指定返回键 0为指定排除键db.a.find({$or:[{"username":"admin"},{"username": "mongod"}]})  select * from a where username='admin' or username='mongod'db.a.find({"num": {$gt:90}, $or: [{"username": "admin"},{"username": "mongod"}]})  select * from num>90 and (username='admin' or username='mongod')db.a.find({"id" : {"$in" : [10, 11, 12]}}) select * from a where id in (10,11,12)  
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,499
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,914
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,746
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,503
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,142
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,305