在max&linux里面直接在终端里面输入下面的语句就可让python支持mongo python
pip install pymongo linux
直接在交互终端里面输入下面的语句,加载pygmon模块 git
from pymongo import MongoClient github
使用MongoClient 建立一个链接: mongodb
client=MongoClient() 数据库
也能够经过传入参数进行选项设置 json
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]] ui
具体的内容能够参与MongoDB URL spa
mongo的数据库不用事前建立,当你调用cient.DatabaseName,通常DatabaseName没有建立它会自动帮助建立一个数据库,第二次这样访问的时候就会直接建立的是以前建立的对象 rest
b = client.primer
你也能够经过下面的方式来进行数据的访问
db=client['primer']
在mongo里面插入数据有两个方法insert_one()和insert_many(),前面的方式单次只能插入一个文档,后面的方法能够插入多个文档内容。假若插入的集合没有事先定义,MongoDB会直接建立。
咱们经过 MongClient模块,而且建立一个链接,链接成功以后选择test数据库
from pymongo import MongoClient
client=MongoClient()
db=client.test2
下面是将一个文档插入到restaurants的集合当中,若是这个restaurants集合没有包含在数据库当中,会自动进行建立。
result = db.restaurants.insert_one( { "address": { "street": "2 Avenue", "zipcode": "10075", "building": "1480", "coord": [-73.9557413, 40.7720266] }, "borough": "Manhattan", "cuisine": "Italian", "grades": [ { "date": datetime.strptime("2014-10-01", "%Y-%m-%d"), "grade": "A", "score": 11 }, { "date": datetime.strptime("2014-01-16", "%Y-%m-%d"), "grade": "B", "score": 17 } ], "name": "Vella", "restaurant_id": "41704620" } )
上面的操做会返回一个InsertOneResult对象,它包含一个_id和inserted_id对象
result.inserted_id
mongodb查询调用的是find()方法,能够传入一些查询的条件来查询想要的结果。返回的是一个可供遍历的结果集合,
对于下面全部的示例用的都是官网上面提示的一个实例文件,primer-dataset.json
下载完成以后经过在终端里面输入下面的语句就能够导入相应的数据
mongoimport --db test --collection restaurants --drop --file primer-dataset.json
对象的格式大概以下:
{'_id': ObjectId('5704d3c3a75b1775d3b21352'),
'address': {'coord': [-73.98513559999999, 40.7676919], 'zipcode': '10019', 'street': 'West 57 Street', 'building': '351'},
'name': 'Dj Reynolds Pub And Restaurant',
'borough': 'Manhattan',
'restaurant_id': '30191841',
'cuisine': 'Irish',
'grades': [{'score': 2, 'date': datetime.datetime(2014, 9, 6, 0, 0), 'grade': 'A'},
{'score': 11, 'date': datetime.datetime(2013, 7, 22, 0, 0), 'grade': 'A'},
{'score': 12, 'date': datetime.datetime(2012, 7, 31, 0, 0), 'grade': 'A'},
{'score': 12, 'date': datetime.datetime(2011, 12, 29, 0, 0), 'grade': 'A'}]}
下面的的语句调用的是find()的方法,返回restaurants当中全部集合中的全部对象.
cursor=db.restaurants.find()
迭代遍历cursor而且将结果进行输出
for document in cursor: print(document)
下面的查找操做仅显示borough为Manhattan的对象.
cursor = db.restaurants.find({"borough": "Manhattan"})
一样经过迭代遍历的方式输出查询的结果
for document in cursor: print(document)
在mongto里面的对象能够在自身包含另外一个集合,好比下面的对象。
上面的primer-dataset.json里面的address是一个集合包含了coord,zipcode,street,building四个数据。
咱们经过下面的语句查询address.zipcode仅为10075的集合
cursor = db.restaurants.find({"address.zipcode": "10075"}) for document in cursor: print(document)
下面的语句查询的是grandes.score大于30的对象
cursor = db.restaurants.find({"grades.score": {"$gt": 30}})
下面的语句查询的是grades.score小于10的集合
cursor = db.restaurants.find({"grades.score": {"$lt": 10}})
在mongo查询的时候能够经过使用(AND)和(OR)来查询
下面的查询的语句至关于,仅显示cuisine为Italian和address.zipcode为10075的数据。只有同时知足才显示。
cursor = db.restaurants.find({"cuisine": "Italian", "address.zipcode": "10075"}) for document in cursor: print(document)
下面的查询的语句至关于,仅显示cuisine为Italian和address.zipcode为10075的数据。两个条件只要知足一个就能够.
cursor = db.restaurants.find({ "$or":[{"cuisine": "Italian", "address.zipcode": "10075"}] } ) for document in cursor: print(document)
能够经过调用 pymongo.ASCENDING()和pymongo.DESCENDING()来指定是按升降序进行排
import pymongo cursor = db.restaurants.find().sort([ ("borough", pymongo.ASCENDING), ("address.zipcode", pymongo.ASCENDING) ])