mongoDB简明教程-python
? MongoDB是一個(gè)介于關(guān)系數(shù)據(jù)庫(kù)和非關(guān)系數(shù)據(jù)庫(kù)之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫(kù)當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫(kù)的。他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似 json的bjson格式,因此可以存儲(chǔ)比較復(fù)雜的數(shù)據(jù)類型。官方網(wǎng)站:http://www.mongodb.org
????????說(shuō)一些基本安裝:首先安裝python環(huán)境這是必須的了吧,具體步驟略。首先安裝mongo環(huán)境,我以window為例,從http://www.mongodb.org/downloads下載Windows 32-bit(我的是32位),解壓到C盤并在C盤建一個(gè)data/db目錄存放數(shù)據(jù)文件,從命令行進(jìn)入到c:/mongodb-win32-i386-1.6.3/bin,然后運(yùn)行:mongod.ext run,就這樣服務(wù)就啟動(dòng)好了。然后開(kāi)始安裝mongo的python模塊:到http://pypi.python.org/pypi/pymongo/下載pymongo-1.9.win32-py2.6.exe然后直接運(yùn)行,安裝完成就可以開(kāi)始編碼了。
????????語(yǔ)言很簡(jiǎn)潔,下面直接上代碼,聰明的你肯定很快就能上手:
| mport pymongo con = pymongo.Connection('localhost', 27017)mydb = con.mydb # new a database mydb.add_user('test', 'test') # add a user mydb.authenticate('test', 'test') # check authmuser = mydb.user # new a tablemuser.save({'id':1, 'name':'test'}) # add a recordmuser.insert({'id':2, 'name':'hello'}) # add a record muser.find_one() # find a recordmuser.find_one({'id':2}) # find a record by querymuser.create_index('id')muser.find().sort('id', pymongo.ASCENDING) # DESCENDING # muser.drop() delete table muser.find({'id':1}).count() # get records numbermuser.find({'id':1}).limit(3).skip(2) # start index is 2 limit 3 recordsmuser.remove({'id':1}) # delet records where id = 1muser.update({'id':2}, {'$set':{'name':'haha'}}) # update one recor |
下面再貼一些類似非python的api參考:?
mongo –path
db.AddUser(username,password) 添加用戶
db.auth(usrename,password) 設(shè)置數(shù)據(jù)庫(kù)連接驗(yàn)證
db.cloneDataBase(fromhost) 從目標(biāo)服務(wù)器克隆一個(gè)數(shù)據(jù)庫(kù)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 復(fù)制數(shù)據(jù)庫(kù)fromdb—源數(shù)據(jù)庫(kù)名稱,todb—目標(biāo)數(shù)據(jù)庫(kù)名稱,fromhost—源數(shù)據(jù)庫(kù)服務(wù)器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 創(chuàng)建一個(gè)數(shù)據(jù)集,相當(dāng)于一個(gè)表
db.currentOp() 取消當(dāng)前庫(kù)的當(dāng)前操作
db.dropDataBase() 刪除當(dāng)前數(shù)據(jù)庫(kù)
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一個(gè)數(shù)據(jù)集合,同用法:db['cname'] or db.cname
db.getCollenctionNames() 取得所有數(shù)據(jù)集合的名稱列表
db.getLastError() 返回最后一個(gè)錯(cuò)誤的提示消息
db.getLastErrorObj() 返回最后一個(gè)錯(cuò)誤的對(duì)象
db.getMongo() 取得當(dāng)前服務(wù)器的連接對(duì)象get the server connection object
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回當(dāng)操作數(shù)據(jù)庫(kù)的名稱
db.getPrevError() 返回上一個(gè)錯(cuò)誤對(duì)象
db.getProfilingLevel() ?什么等級(jí)
db.getReplicationInfo() ?什么信息
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(殺死)在當(dāng)前庫(kù)的當(dāng)前操作
db.printCollectionStats() 返回當(dāng)前庫(kù)的數(shù)據(jù)集狀態(tài)
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回當(dāng)前數(shù)據(jù)庫(kù)是否為共享數(shù)據(jù)庫(kù)
db.removeUser(username) 刪除用戶
db.repairDatabase() 修復(fù)當(dāng)前數(shù)據(jù)庫(kù)
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 0=off,1=slow,2=all
db.shutdownServer() 關(guān)閉當(dāng)前服務(wù)程序
db.version() 返回當(dāng)前程序的版本信息
db.linlin.find({id:10}) 返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)集
db.linlin.find({id:10}).count() 返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)總數(shù)
db.linlin.find({id:10}).limit(2)返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)集從第二條開(kāi)始的數(shù)據(jù)集
db.linlin.find({id:10}).skip(8) 返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)集從0到第八條的數(shù)據(jù)集
db.linlin.find({id:10}).limit(2).skip(8) 返回linlin數(shù)據(jù)集ID=1=的數(shù)據(jù)集從第二條到第八條的數(shù)據(jù)
db.linlin.find({id:10}).sort() 返回linlin數(shù)據(jù)集ID=10的排序數(shù)據(jù)集
db.linlin.findOne([query]) 返回符合條件的一條數(shù)據(jù)
db.linlin.getDB() 返回此數(shù)據(jù)集所屬的數(shù)據(jù)庫(kù)名稱
db.linlin.getIndexes() 返回些數(shù)據(jù)集的索引信息
db.linlin.group({key:…,initial:…,reduce:…[,cond:...]})
db.linlin.mapReduce(mayFunction,reduceFunction,
)
db.linlin.remove(query) 在數(shù)據(jù)集中刪除一條數(shù)據(jù)
db.linlin.renameCollection(newName) 重命名些數(shù)據(jù)集名稱
db.linlin.save(obj) 往數(shù)據(jù)集中插入一條數(shù)據(jù)
db.linlin.stats() 返回此數(shù)據(jù)集的狀態(tài)
db.linlin.storageSize() 返回此數(shù)據(jù)集的存儲(chǔ)大小
db.linlin.totalIndexSize() 返回此數(shù)據(jù)集的索引文件大小
db.linlin.totalSize() 返回些數(shù)據(jù)集的總大小
db.linlin.update(query,object[,upsert_bool])在此數(shù)據(jù)集中更新一條數(shù)據(jù)
db.linlin.validate() 驗(yàn)證此數(shù)據(jù)集
db.linlin.getShardVersion() 返回?cái)?shù)據(jù)集共享版本號(hào)
db.linlin.find({‘name’:'foobar’}) select * from linlin where name=’foobar’
db.linlin.find() select * from linlin
db.linlin.find({‘ID’:10}).count() select count(*) from linlin where ID=10
db.linlin.find().skip(10).limit(20) 從查詢結(jié)果的第十條開(kāi)始讀20條數(shù)據(jù) select * from linlin limit 10,20 ———-mysql
db.linlin.find({‘ID’:{in:[25,35,45]}})?select?*?from?linlin?where?ID?in?(25,35,45)?
db.linlin.find().sort({‘ID’:-1})?select?*?from?linlin?order?by?ID?desc?
db.linlin.distinct(‘name’,{‘ID’:{lt:20}}) select distinct(name) from linlin where ID<20
db.linlin.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})
select name,sum(marks) from linlin group by name
db.linlin.find('this.ID<20′,{name:1}) select name from linlin where ID<20
db.linlin.insert({'name':'foobar’,'age':25}) insert into linlin ('name','age’)values('foobar',25)
db.linlin.insert({'name':'foobar’,'age':25,’email’:'cclove2@163.com’})
db.linlin.remove({}) delete * from linlin
db.linlin.remove({'age':20}) delete linlin where age=20
db.linlin.remove({'age':{lt:20}})?delete?linlin?where?age<20?
db.linlin.remove({'age':{lte:20}}) delete linlin where age<=20
db.linlin.remove({'age':{gt:20}})?delete?linlin?where?age>20?
db.linlin.remove({‘a(chǎn)ge’:{gte:20}}) delete linlin where age>=20
db.linlin.remove({‘a(chǎn)ge’:{ne:20}})?delete?linlin?where?age!=20?
db.linlin.update({‘name’:'foobar’},{‘set’:{‘a(chǎn)ge’:36}}) update linlin set age=36 where name=’foobar’
db.linlin.update({‘name’:'foobar’},{‘$inc’:{‘a(chǎn)ge’:3}}) update linlin set age=age+3 where name=’foobar’
總結(jié)
以上是生活随笔為你收集整理的mongoDB简明教程-python的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Hadoop Hive sql语法详解
- 下一篇: Python调用MongoDB使用心得