MongoDB-based Python enhancement and deletion check (CRUD)

Source: Internet
Author: User

1, download MongoDB python drive, http://pypi.python.org/pypi/pymongo/, select the appropriate egg or EXE installation according to the operating system and Python platform version.

2, create a new PY script (can be installed under Eclipse Pydev plug-in, very convenient)

#coding =utf-8 Import Pymongo connection = Pymongo. Connection ("192.168.0.101", 27017) db = Connection.python Print db.name print db.my_collection

Db.my_collection.save ({"X": 10})

Db.my_collection.save ({"X": 8}) Db.my_collection.save ({"X": One}) Db.my_collection.save ({"X":}) Db.my_ Collection.save ({"x": +, "Y": 3})

Print Db.my_collection.find_one () print ' All data, x value ' for item in Db.my_collection.find (): Print item["x"] Db.my_collecti On.remove ({"Y": 3}); print ' Delete, all data, x value ' for item in Db.my_collection.find (): Print item["x"] Db.my_collection.update ({"X": 8},{"X": 2}) pr int ' Update, all data, x value ' for item in Db.my_collection.find (): Print item["x"] Print Db.my_collection.create_index ("x") #创建索 The index query for item in Db.my_collection.find (). Sort ("X", Pymongo. Ascending): Print item["x"]

Print [item["x"] for item in Db.my_collection.find (). Limit (2). Skip (1)]

3,mongodb do not need to manually build the database and tables, the program will be automatically created before inserting, execution results:

Python Collection (Database (Connection (' 192.168.0.101 ', 27017), U ' python '), U ' my_collection ') {u ' x ': Ten, U ' _id ': ObjectId (' 4d2961921e66a10a94000000 ')} All data, X value 10 8 11 13 14 After deletion, all data, x value 10 8 11 updated, all data, x value 13 each x_1 2 2 10 11 [2, 11]

4, note: More Pymongo documentation, follow http://api.mongodb.org/python/1.9%2B/api/index.html

MongoDB-based Python enhancement and deletion check (CRUD)

Related Article

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.