pymongo库的简单用法

作者 张旭 日期 2017-02-09
pymongo库的简单用法
from pymongo import MongoClient
import pymongo
client = MongoClient()
client
MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True)

导入测试数据

下载: https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json

mongoimport –db test –collection restaurants –drop –file ~/downloads/primer-dataset.json

pymongo 使用

# 连接
client = MongoClient("mongodb://localhost:27017")
client = MongoClient()
db = client.test
db = client['test']

获取集合

db.restaurants
Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'test'), 'restaurants')

插入数据

插入一个Document

from datetime import datetime
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"
}
)
## 插入的ID
result.inserted_id
ObjectId('589bd8dd421aa917e92e3057')

插入多个Document

db.restaurants.count()
25359
result = db.restaurants.insert_many([{'x': i,'y':34} for i in range(3)])
result.inserted_ids
[ObjectId('589bd899421aa917e92e3050'),
 ObjectId('589bd899421aa917e92e3051'),
 ObjectId('589bd899421aa917e92e3052')]
[{'x': i,'y':34} for i in range(3)]
[{'x': 0, 'y': 34}, {'x': 1, 'y': 34}, {'x': 2, 'y': 34}]

查找 查询 数据

查询所有数据

cursor = db.restaurants.find()
cursor[20]
{'_id': ObjectId('589bd9efd1dc797515fdb021'),
 'address': {'building': '129-08',
  'coord': [-73.839297, 40.78147],
  'street': '20 Avenue',
  'zipcode': '11356'},
 'borough': 'Queens',
 'cuisine': 'Delicatessen',
 'grades': [{'date': datetime.datetime(2014, 8, 16, 0, 0),
   'grade': 'A',
   'score': 12},
  {'date': datetime.datetime(2013, 8, 27, 0, 0), 'grade': 'A', 'score': 9},
  {'date': datetime.datetime(2012, 9, 20, 0, 0), 'grade': 'A', 'score': 7},
  {'date': datetime.datetime(2011, 9, 29, 0, 0), 'grade': 'A', 'score': 10}],
 'name': "Sal'S Deli",
 'restaurant_id': '40361618'}

相等条件查询 Specify Equality Conditions¶

查询顶级字段数据

cursor = db.restaurants.find({"borough": "Manhattan"})
cursor.count()
10259

通过内嵌字段查询 Query by a Field in an Embedded Document,

cursor = db.restaurants.find({"address.zipcode": "10075"})
cursor[2]
{'_id': ObjectId('589bd9f0d1dc797515fdb166'),
 'address': {'building': '1133',
  'coord': [-73.958885, 40.7745559],
  'street': 'Lexington Avenue',
  'zipcode': '10075'},
 'borough': 'Manhattan',
 'cuisine': 'Italian',
 'grades': [{'date': datetime.datetime(2014, 8, 11, 0, 0),
   'grade': 'A',
   'score': 11},
  {'date': datetime.datetime(2013, 12, 10, 0, 0), 'grade': 'A', 'score': 9},
  {'date': datetime.datetime(2013, 6, 10, 0, 0), 'grade': 'A', 'score': 12},
  {'date': datetime.datetime(2012, 6, 8, 0, 0), 'grade': 'A', 'score': 13},
  {'date': datetime.datetime(2012, 1, 25, 0, 0), 'grade': 'A', 'score': 8},
  {'date': datetime.datetime(2011, 9, 13, 0, 0), 'grade': 'A', 'score': 12}],
 'name': 'Don Filippo Restaurant',
 'restaurant_id': '40372417'}

通过数组内字段查询 Query by a Field in an Array

cursor = db.restaurants.find({"grades.grade": "B"})

Note: 数组中有一个元素满足条件,则此Document满足条件
The above queries for documents whose grades array contains an embedded document with a field grade equal to “B”.

应用操作符查询 Specify Conditions with Operators

查询结构: { : { : } }

> 大于 Greater Than Operator ($gt)

cursor = db.restaurants.find({"grades.score": {"$gt": 30}})

< 小于操作 Less Than Operator ($lt)

cursor = db.restaurants.find({"grades.score": {"$lt": 10}})

$gte,>=; $lte <=;
其他操作符 ,详见 https://docs.mongodb.com/manual/reference/operator/query/

组合查询 Combine Conditions

通过 and or not 等组合查询条件

Logical AND

#直接添加 条件

cursor = db.restaurants.find({"cuisine": "Italian", "grades.score": {"$lt": 10}})
cursor = db.restaurants.find({"cuisine": "Italian", "address.zipcode": "10075"})
cursor = db.restaurants.find({"$and": [{"cuisine": "Italian"}, {"address.zipcode":"10075"}]})

Logical OR

cursor = db.restaurants.find(
{"$or": [{"cuisine": "Italian"}, {"address.zipcode": "10075"}]})

排序 Sort Query Results

cursor = db.restaurants.find().sort([
("borough", pymongo.ASCENDING),
("address.zipcode", pymongo.ASCENDING)
])

Update Data with PyMongo

使用更新操作符,$set,$inc,$mul,$rename,$unset……https://docs.mongodb.com/manual/reference/operator/update/

更新特定键 Update Specific Fields

'''
updates the first document with name equal to "Juni",
using the $set operator to update the cuisine field and
the $currentDate operator to update the lastModified field with the current date.
'''
result = db.restaurants.update_one(
{"name": "Juni"},
{
"$set": {
"cuisine": "American (New)"
},
"$currentDate": {"lastModified": True}
}
)

返回一个UpdateResult, 具有本次更新的相关数据,如result.modified_count

更新内嵌的键值对 Update an Embedded Field

#To update a field within an embedded document, use the dot notation
result = db.restaurants.update_one(
{"restaurant_id": "41156888"},
{"$set": {"address.street": "East 31st Street"}}
)
## Update Multiple Documents
'''
The following operation updates all documents that have address.
zipcode field equal to "10016" and cuisine field equal to "Other",
setting the cuisine field to "Category To Be Determined"
and the lastModified field to the current date.
'''
result = db.restaurants.update_many(
{"address.zipcode": "10016", "cuisine": "Other"},
{
"$set": {"cuisine": "Category To Be Determined"},
"$currentDate": {"lastModified": True}
}
)
## Replace a Document
result = db.restaurants.replace_one(
{"restaurant_id": "41704620"},
{
"name": "Vella 2",
"address": {
"coord": [-73.9557413, 40.7720266],
"building": "1480",
"street": "2 Avenue",
"zipcode": "10075"
}
}
)

Note:
Mongodb 对于单个document 满足原子性,对于一次操作很多文档,并不满足原子性。如果想使操作很多文档也满足原子性,使用$isolated

详见:https://docs.mongodb.com/manual/core/write-operations-atomicity/

删除数据 Remove Data

Use the delete_one() method and the delete_many()

删除所有符合条件数据 Remove All Documents That Match a Condition

result = db.restaurants.delete_many({"borough": "Manhattan"})

删除所有数据 Remove All Documents

result = db.restaurants.delete_many({})

删除整个集合 Drop a Collection

db.restaurants.drop()

数据聚合 Data Aggregation

Use the aggregate() method to perform a stage-based aggregation.
The aggregate() method accepts as its argument an array of stages,
where each stage, processed sequentially, describes a data processing step.格式:
db.collection.aggregate([, , …])
$group $match $sort $count ….. https://docs.mongodb.com/manual/meta/aggregation-quick-reference/#group-operators

Group Documents by a Field and Calculate Count

cursor = db.restaurants.aggregate(
[
{"$group": {"_id": "$borough", "count": {"$sum": 1}}}
]
)

Filter and Group Documents

cursor = db.restaurants.aggregate(
[
{"$match": {"borough": "Queens", "cuisine": "Brazilian"}},
{"$group": {"_id": "$address.zipcode", "count": {"$sum": 1}}}
]
)

Indexes with PyMongo

db.restaurants.create_index([
("cuisine", pymongo.ASCENDING),
("address.zipcode", pymongo.DESCENDING)
])
'cuisine_1_address.zipcode_-1'

参考: https://docs.mongodb.com/getting-started/python/