Mogodb and Pymongo

Source: Internet
Author: User

Ubuntu Installation: Apt-get install Mongdb
If you need to use Python link MongoDB also need to install Pymogo:pip install Pymongo
1. Try the Pymongo link MongoDB first
>import Pymongo
>con=pymongo. Connection (' localhost ', 27017)
>db = Con.test
>table=db.foo
>table.insert ({' py ': ' Pymongo '}) #插入一条数据

Or simplify the point:
Table=pymongo. Connection (' localhost ', 27017). Test.foo
Table.insert ({' KKK ': ' kkkk ', ' pppp ': ' PPPP '})

2. Common commands for Rights management
Use admin #进入数据库admin
Db.adduser (' name ', ' pwd ') #增加或修改用户密码
Db.system.users.find () #查看用户列表
Db.auth (' name ', ' pwd ') #用户认证 this one returns 1 for certification success, only successful authentication can operate on the database
Db.removeuser (' name ') #删除用户
Show Users #查看所有用户
Show DBS #查看所有数据库 Show Collections #查看所有的collection (Similar enquiry form, show tables)
Db.printcollectionstats () #查看各collection的状态
Db.printreplicationinfo () #查看主从复制状态
3: Link Database test
Mogodb>use test #一般默认就是链接test数据库的, can not write, link other libraries need Mogodb>db.foo.insert ({data: ' 2014-9-21 '}) #向foo集合 (table) Insert a Bar data
Mogodb>db.foo.update ({a:1},{a:2}) #向foo集合更新一条数据, if there is no match to {a:1}, add a data {a:2}
Mogodb> db.users.update ({age:25}, {$set: {name: ' ChangeName '}}, False, True);
Equivalent to: Update users set name = ' ChangeName ' where age = 25;
Mogodb> db.users.update ({name: ' Lisi '}, {$inc: {age:50}}, False, True);
Equivalent to: Update users set age = Age + where name = ' Lisi ';
Mogodb> db.users.update ({name: ' Lisi '}, {$inc: {age:50}, $set: {name: ' HoHo '}}, False, True);
Equivalent to: Update users set age = Age +, name = ' HoHo ' WHERE name = ' Lisi ';

Mogodb>db.foo.find () #向foo集合 (table) query strip data;
Mogodb>db.foo.findone () #向foo集合 (table) query bar data, Jons display
Mogodb>db.foo.remove ({a:1}) #向foo集合 (table) deletes a single piece of data
Mogodb>db.foo.remove () #向foo集合 (table) Delete all data
----------------
Use KKK #链接数据库kkk, the database is automatically created when you insert the database KKK
Db.tables.insert ({}) #创建集合 (table) tables and insert data; Insert empty data if set exists
Db.tables.save ({}) #创建集合 (table) tables, and save the data, such as the collection exists test new empty data and save
Db.tables.drop () #删除oo集合 (table)
Db.getname () #获取当前使用的数据库名称1
DB #获取当前使用的数据库名称2
Db.dropdatabase () #删除当前使用的数据库
db.repairdatabase ();#修复当前使用的数据库
db.clonedatabase ("127.0.0.1");   #从指定机器克隆数据库
db.copydatabase ("mydb",  "temp",  "127.0.0.1");  #从指定机器复制指定数据库到某库
Db.getmongo (); Get the machine address of the link db




Mogodb and Pymongo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.