First need to install Pymongo module, module download address
https://pypi.python.org/pypi/pymongo/#downloads
Select the appropriate version, to install, after the installation is completed, you can link your mongodb.
pymongodb-2.3.tar.gz Download: http://download.csdn.net/detail/wanzhuan2010/6889887
#encoding: utf=8
import Pymongo
Connection=pymongo. Connection (' 10.32.38.50 ', 27017)
#选择myblog库
db=connection.myblog
# Use the Users collection
collection=db.users
# Add a single piece of data to the collection
user = {"name": "Cui", "Age": "Ten"}
collection.insert (user)
#同时添加多条数据到集合中
users=[ {' name ': ' Cui ', ' age ': ' 9 '},{' name ': ' Cui ', ' age ': ' One '}]
Collection.insert (users)
#查询单条记录
Print Collection.find_one ()
#查询所有记录 for
data in Collection.find ():
print data
#查询此集合中数据条数
Print Collection.count ()
#简单参数查询 for
data in Collection.find ({"Name": "1"}):
print Data
#使用find_ One gets a record
print Collection.find_one ({"Name": "1"})
#高级查询
print "__________________________________ ________ "
print" Collection.find ({"Age": {"$GT": "The"}}) '
print ' ___________________________________ ___ ' for
data in Collection.find ({' age ': {' $gt ': ' Ten}}]. Sort ("Age"):
print Data
# View all collections under DB
Print Db.collection_names ()
Reference: http://www.oschina.net/question/54100_27233