1 mongodb安裝
1.centos
(1)cd /etc/yum.reposd/
(2)vi mongodb-org.repo
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
(3)yum install -y mongodb -org(需要網(wǎng)絡(luò))
(4)啟動(dòng)mongodb
service mongod start
(5)配置遠(yuǎn)程機(jī)器可以訪問
vi /etc /mongod.conf
將 blind 127.0.01 這一行注釋掉即可
關(guān)閉防火墻和selinux
service iptables stop
(6)遠(yuǎn)程連接
mogo 192.168.1.104 :27107use
2.windows
安裝MongoDB
官網(wǎng)下載msi安裝包
https://www.mongodb.com/dr/fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-3.4.10-signed.msi/download
安裝,一路next即可
添加環(huán)境變量
將MongoDB\Server\3.4\bin 添加到環(huán)境變量中
新建文件夾
c:\data\db 數(shù)據(jù)庫存放目錄
c:\data\log 日志存放目錄
啟動(dòng)服務(wù)
mongod.exe --dbpath c:\data\db
啟動(dòng)成功,默認(rèn)MongoDB監(jiān)聽的端口是27017
【注】如果是默認(rèn)安裝路徑,路徑需要用引號(hào)括起來才行
【注】或者將data文件夾放到c盤或者d盤根目錄下
【注】用管理員命令行,并且有空格的用雙引號(hào)引起來即可
啟動(dòng)客戶端
mongo.exe
連接成功進(jìn)入,默認(rèn) 127.0.0.1:27017
將MongoDB設(shè)置為windows服1.務(wù)
在終端下輸入:
mongod --dbpath "c:\data\db" --logpath "c:\data\log\MongoDB.log" --install --serviceName "MongoDB"
啟動(dòng)服務(wù) net start MongoDB
關(guān)閉服務(wù) net stop MongoDB
卸載windows服務(wù)MongoDB
mongod --dbpath "c:\data\db" --logpath "c:\data\log\MongoDB.log" --r
2 mongodb指令
一、操作mongodb
- 創(chuàng)建數(shù)據(jù)庫
use 數(shù)據(jù)庫名 - 刪除數(shù)據(jù)庫(前提:使用當(dāng)前數(shù)據(jù)庫 use 數(shù)據(jù)庫名)
db.dropDatabase() - 查看所有數(shù)據(jù)庫
show dbs - 查看當(dāng)前正在使用的數(shù)據(jù)庫
a、db
b、db.getName - 斷開連接
exit
二、集合操作
- 查看當(dāng)前數(shù)據(jù)庫下有哪些集合
show collections - 創(chuàng)建集合
a、db.createCollection('集合名')
b、db.集合名.insert(文檔)
例:db.student.insert({name:"tom",age:18})
+刪除當(dāng)前數(shù)據(jù)庫的集合
db.集合名.drop
三、文檔操作
- 插入文檔
a、使用insert()方法
b、使用save()方法
db.集合 名.save({...})
注:如果指定_id字段,則是更新文檔 - 文檔更新
a、update()方法用于更新已存在的文檔
db.集合名.update(
query, # 查詢條件
update, # 更新什么
{ # 附加功能
upsert:<boolean>,
multi:<boolean>
}
)
示例:
db.student.update({name:"劉德華"},{$set:{age:25}})
累加:db.student.update({name:"劉德華"},{$inc:{age:25}})
全改:db.student.update({name:"劉德華"},{$set:{age:42}},{multi:true})