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