pymongo api

来源:互联网 发布:2015淘宝新店扶持 编辑:程序博客网 时间:2024/06/06 03:24
01importpymongo
02con =pymongo.Connection('localhost',27017)
03mydb =con.mydb # new a database
04mydb.add_user('test','test') # add a user
05mydb.authenticate('test','test') # check auth
06muser =mydb.user # new a table
07   
08muser.save({'id':1,'name':'test'})# add a record
09muser.insert({'id':2,'name':'hello'})# add a record
10muser.find_one() # find a record
11muser.find_one({'id':2})# find a record by query
12   
13muser.create_index('id')
14muser.find().sort('id', pymongo.ASCENDING)# DESCENDING
15# muser.drop() delete table
16muser.find({'id':1}).count()# get records number
17muser.find({'id':1}).limit(3).skip(2)# start index is 2 limit 3 records
18muser.remove({'id':1})# delet records where id = 1
19   
20muser.update({'id':2}, {'$set':{'name':'haha'}})# update one record

 

 

还有一些语法:

01mongo –path
02db.AddUser(username,password) 添加用户
03db.auth(usrename,password) 设置数据库连接验证
04db.cloneDataBase(fromhost) 从目标服务器克隆一个数据库
05db.commandHelp(name) returns the help forthe command
06db.copyDatabase(fromdb,todb,fromhost) 复制数据库fromdb—源数据库名称,todb—目标数据库名称,fromhost—源数据库服务器地址
07db.createCollection(name,{size:3333,capped:333,max:88888}) 创建一个数据集,相当于一个表
08db.currentOp() 取消当前库的当前操作
09db.dropDataBase() 删除当前数据库
10db.eval(func,args) run code server-side
11db.getCollection(cname) 取得一个数据集合,同用法:db['cname']ordb.cname
12db.getCollenctionNames() 取得所有数据集合的名称列表
13db.getLastError() 返回最后一个错误的提示消息
14db.getLastErrorObj() 返回最后一个错误的对象
15db.getMongo() 取得当前服务器的连接对象get the server connectionobject
16db.getMondo().setSlaveOk() allow this connection to readfromthen nonmaster membr of a replica pair
17db.getName() 返回当操作数据库的名称
18db.getPrevError() 返回上一个错误对象
19db.getProfilingLevel() ?什么等级
20db.getReplicationInfo() ?什么信息
21db.getSisterDB(name) get the db at the same server as this onew
22db.killOp() 停止(杀死)在当前库的当前操作
23db.printCollectionStats() 返回当前库的数据集状态
24db.printReplicationInfo()
25db.printSlaveReplicationInfo()
26db.printShardingStatus() 返回当前数据库是否为共享数据库
27db.removeUser(username) 删除用户
28db.repairDatabase() 修复当前数据库
29db.resetError()
30db.runCommand(cmdObj) run a database command.ifcmdObj isa string, turns it into {cmdObj:1}
31db.setProfilingLevel(level) 0=off,1=slow,2=all
32db.shutdownServer() 关闭当前服务程序
33db.version() 返回当前程序的版本信息
34 
35db.linlin.find({id:10}) 返回linlin数据集ID=10的数据集
36db.linlin.find({id:10}).count() 返回linlin数据集ID=10的数据总数
37db.linlin.find({id:10}).limit(2)返回linlin数据集ID=10的数据集从第二条开始的数据集
38db.linlin.find({id:10}).skip(8) 返回linlin数据集ID=10的数据集从0到第八条的数据集
39db.linlin.find({id:10}).limit(2).skip(8) 返回linlin数据集ID=1=的数据集从第二条到第八条的数据
40db.linlin.find({id:10}).sort() 返回linlin数据集ID=10的排序数据集
41db.linlin.findOne([query]) 返回符合条件的一条数据
42db.linlin.getDB() 返回此数据集所属的数据库名称
43db.linlin.getIndexes() 返回些数据集的索引信息
44db.linlin.group({key:…,initial:…,reduce:…[,cond:...]})
45db.linlin.mapReduce(mayFunction,reduceFunction,
46)
47db.linlin.remove(query) 在数据集中删除一条数据
48db.linlin.renameCollection(newName) 重命名些数据集名称
49db.linlin.save(obj) 往数据集中插入一条数据
50db.linlin.stats() 返回此数据集的状态
51db.linlin.storageSize() 返回此数据集的存储大小
52db.linlin.totalIndexSize() 返回此数据集的索引文件大小
53db.linlin.totalSize() 返回些数据集的总大小
54db.linlin.update(query,object[,upsert_bool])在此数据集中更新一条数据
55db.linlin.validate() 验证此数据集
56db.linlin.getShardVersion() 返回数据集共享版本号
57db.linlin.find({‘name’:'foobar’}) select* fromlinlin where name=’foobar’
58db.linlin.find() select *fromlinlin
59db.linlin.find({‘ID’:10}).count() select count(*)fromlinlin where ID=10
60db.linlin.find().skip(10).limit(20) 从查询结果的第十条开始读20条数据 select *fromlinlin limit 10,20———-mysql
61db.linlin.find({‘ID’:{$in:[25,35,45]}}) select *fromlinlin where IDin(25,35,45)
62db.linlin.find().sort({‘ID’:-1}) select *fromlinlin order by IDdesc
63db.linlin.distinct(‘name’,{‘ID’:{$lt:20}}) select distinct(name)fromlinlin where ID<20
64db.linlin.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})
65select name,sum(marks)fromlinlin group by name
66db.linlin.find('this.ID<20′,{name:1}) select name fromlinlin where ID<20
67db.linlin.insert({'name':'foobar’,'age':25}) insert into linlin ('name','age’)values('foobar',25)
68db.linlin.insert({'name':'foobar’,'age':25,’email’:'cclove2@163.com’})
69db.linlin.remove({}) delete *fromlinlin
70db.linlin.remove({'age':20}) delete linlin where age=20
71db.linlin.remove({'age':{$lt:20}}) delete linlin where age<20
72db.linlin.remove({'age':{$lte:20}}) delete linlin where age<=20
73db.linlin.remove({'age':{$gt:20}}) delete linlin where age>20
74db.linlin.remove({‘age’:{$gte:20}}) delete linlin where age>=20
75db.linlin.remove({‘age’:{$ne:20}}) delete linlin where age!=20
76db.linlin.update({‘name’:'foobar’},{‘$set’:{‘age’:36}}) update linlinset age=36where name=’foobar’
77db.linlin.update({‘name’:'foobar’},{‘$inc’:{‘age’:3}}) update linlinset age=age+3where name=’foobar’
原创粉丝点击