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)