SQL術語/概念 | MongoDB術語/概念 | 解釋/說明 |
---|---|---|
database | database | 數據庫 |
table | collection | 數據庫表/集合 |
row | document | 數據記錄行/文檔 |
column | field | 數據字段/域 |
index | index | 索引 |
table joins | 錶鏈接,MongoDB不支持 | |
primary key | primary key | 主鍵,MongoDB自動將_id字段設置爲主鍵 |
{'name':'guojing','gender':'男'}
{'name':'guojing','gender':'男'} {'name':'huangrong','age':18} {'book':'shuihuzhuan','heros':'108'}
tar -zxvf mongodb-linux-x86_64-ubuntu1604-3.4.0.tgz
sudo mv -r mongodb-linux-x86_64-ubuntu1604-3.4.0/ /usr/local/mongodb
export PATH=/usr/local/mongodb/bin:$PATH
默認端口27017html
啓動python
sudo service mongod start
sudo service mongod stop
mongo
db查看當前數據庫名稱 db.stats()查看當前數據庫信息
exit 或ctrl+c
1.2 數據庫操做linux
db
show dbs
use 數據庫名稱
1.3. 集合操做db.dropDatabase()
db.createCollection(name, options)
db.createCollection("stu")
db.createCollection("sub", { capped : true, size : 10 } )
show collections
db.集合名稱.drop()
db.集合名稱.insert(document)
db.stu.insert({name:'gj',gender:1})
s1={_id:'20160101',name:'hr'} s1.gender=0 db.stu.insert(s1)
db.集合名稱.find()
db.集合名稱.update( <query>, <update>, {multi: <boolean>} )
db.stu.update({name:'hr'},{name:'mnc'})
db.stu.insert({name:'hr',gender:0}) db.stu.update({name:'hr'},{$set:{name:'hys'}})
db.stu.update({},{$set:{gender:0}},{multi:true})
db.集合名稱.save(document)
若是文檔的_id已經存在則修改,若是文檔的_id不存在則添加web
例6正則表達式
db.stu.save({_id:'20160102','name':'yk',gender:1})
db.stu.save({_id:'20160102','name':'wyk'})
db.集合名稱.remove( <query>, { justOne: <boolean> } )
db.stu.remove({gender:0},{justOne:true})
db.stu.remove({})
db.createCollection('sub',{capped:true,size:10})
db.sub.insert({title:'linux',count:10}) db.sub.find()
db.sub.insert({title:'web',count:15}) db.sub.find()
db.sub.insert({title:'sql',count:8}) db.sub.find()
db.sub.insert({title:'django',count:12}) db.sub.find()
db.sub.insert({title:'python',count:14}) db.sub.find()
db.集合名稱.find({條件文檔})
db.集合名稱.findOne({條件文檔})
db.集合名稱.find({條件文檔}).pretty()
db.stu.find({name:'gj'})
db.stu.find({age:{$gte:18}})
db.stu.find({age:{$gte:18},gender:1})
db.stu.find({$or:[{age:{$gt:18}},{gender:1}]})
db.stu.find({$or:[{age:{$gte:18}},{gender:1}],name:'gj'})
db.stu.find({age:{$in:[18,28]}})
db.stu.find({name:/^黃/}) db.stu.find({name:{$regex:'^黃'}}})
1.6.1 Limit和Skipdb.stu.find({$where:function(){return this.age>20}})
db.集合名稱.find().limit(NUMBER)
db.stu.find().limit(2)
db.集合名稱.find().skip(NUMBER)
db.stu.find().skip(2)
方法limit()和skip()能夠一塊兒使用,不分前後順序sql
建立數據集mongodb
for(i=0;i<15;i++){db.t1.insert({_id:i})}
1.6.2db.stu.find().limit(4).skip(5) 或 db.stu.find().skip(5).limit(4)
db.集合名稱.find({},{字段名稱:1,...})
db.stu.find({},{name:1,gender:1})
1.6.3db.stu.find({},{_id:0,name:1,gender:1})
db.集合名稱.find().sort({字段:1,...})
1.6.4db.stu.find().sort({gender:-1,age:1})
db.集合名稱.find({條件}).count()
db.集合名稱.count({條件})
db.stu.find({gender:1}).count()
1.6.5db.stu.count({age:{$gt:20},gender:1})
db.集合名稱.distinct('去重字段',{條件})
1.7db.stu.distinct('gender',{age:{$gt:18}})
2. shell
2.1數據庫
db.集合名稱.aggregate([{管道:{表達式}}])
ps ajx | grep mongo
表達式:'$列名'
2.1.1
db.stu.aggregate([ {$group: { _id:'$gender', counter:{$sum:1} } } ])
db.stu.aggregate([ {$group: { _id:null, counter:{$sum:1}, avgAge:{$avg:'$age'} } } ])
db.stu.aggregate([ {$group: { _id:'$gender', name:{$push:'$name'} } } ])
2.1.2db.stu.aggregate([ {$group: { _id:'$gender', name:{$push:'$$ROOT'} } } ])
db.stu.aggregate([ {$match:{age:{$gt:20}}} ])
db.stu.aggregate([ {$match:{age:{$gt:20}}}, {$group:{_id:'$gender',counter:{$sum:1}}} ])
2.1.3
db.stu.aggregate([ {$project:{_id:0,name:1,age:1}} ])
2.1.4db.stu.aggregate([ {$group:{_id:'$gender',counter:{$sum:1}}}, {$project:{_id:0,counter:1}} ])
b.stu.aggregate([{$sort:{age:1}}])
2.db.stu.aggregate([ {$group:{_id:'$gender',counter:{$sum:1}}}, {$sort:{counter:-1}} ])
2.1.5
db.stu.aggregate([{$limit:2}])
db.stu.aggregate([{$skip:2}])
db.stu.aggregate([ {$group:{_id:'$gender',counter:{$sum:1}}}, {$sort:{counter:1}}, {$skip:1}, {$limit:1} ])
2.1.6django
db.集合名稱.aggregate([{$unwind:'$字段名稱'}])
db.t2.insert({_id:1,item:'t-shirt',size:['S','M','L']})
db.t2.aggregate([{$unwind:'$size'}])
db.inventory.aggregate([{ $unwind:{ path:'$字段名稱', preserveNullAndEmptyArrays:<boolean>#防止數據丟失 } }])
db.t3.insert([ { "_id" : 1, "item" : "a", "size": [ "S", "M", "L"] }, { "_id" : 2, "item" : "b", "size" : [ ] }, { "_id" : 3, "item" : "c", "size": "M" }, { "_id" : 4, "item" : "d" }, { "_id" : 5, "item" : "e", "size" : null } ])
db.t3.aggregate([{$unwind:'$size'}])
2.2安全db.t3.aggregate([{$unwind:{path:'$sizes',preserveNullAndEmptyArrays:true}}])
use admin db.createUser({ user:'admin', pwd:'123', roles:[{role:'root',db:'admin'}] })
sudo vi /etc/mongod.conf
security: authorization: enabled
sudo service mongod stop sudo service mongod start
mongo -u 'admin' -p '123' --authenticationDatabase 'admin'
use test1 show users
db.createUser({ user:'t1', pwd:'123', roles:[{role:'readWrite',db:'test1'}] })
mongo -u t1 -p 123 --authenticationDatabase test1
切換數據庫,執行命令查看效果
修改用戶:能夠修改pwd、roles屬性
2.3db.updateUser('t1',{pwd:'456'})
mkdir t1 mkdir t2
mongod --bind_ip 192.168.196.128 --port 27017 --dbpath ~/Desktop/t1 --replSet rs0 mongod --bind_ip 192.168.196.128 --port 27018 --dbpath ~/Desktop/t2 --replSet rs0
mongo --host 192.168.196.128 --port 27017
rs.initiate()
rs.status()
rs.add('192.168.196.128:27018')
mongo --host 192.168.196.128 --port 27018
use test1 for(i=0;i<10;i++){db.t1.insert({_id:i})} db.t1.find()
rs.slaveOk() db.t1.find()
rs.remove('192.168.196.128:27018')
2.4
mongodump -h dbhost -d dbname -o dbdirectory
sudo mkdir test1bak sudo mongodump -h 192.168.196.128:27017 -d test1 -o ~/Desktop/test1bak
mongorestore -h dbhost -d dbname --dir dbdirectory
2.5mongorestore -h 192.168.196.128:27017 -d test2 --dir ~/Desktop/test1bak/test1
進入虛擬環境 sudo pip install pymongo 或源碼安裝 python setup.py
import pymongo
client=pymongo.MongoClient("localhost", 27017)
db=client.test1
stu = db.stu
s1={name:'gj',age:18} s1_id = stu.insert_one(s1).inserted_id
s2=stu.find_one()
for cur in stu.find(): print cur
cur=stu.find() cur.next() cur.next() cur.next()
2.6print stu.count()