PyMongo操作MongoDB的一些基本示例

浏览: 3725

最近在工作中使用到了MongoDB, 就总结了一下基本用法,供大家参考,以示例为主。

MongoDB版本是3.2.6(测试使用的标准docker image),Python版本是3.5.1,使用了PyMongo访问MongoDB。

0.MongoDB一些基本概念

就简单和数据库类比了一下,更具体大家自己查吧:

database:就是oracle里面schema

collection对应的就是表,只是不需要先定义表结构,用的时候定义即可(带来的问题就是取的时候可能要验证)。

安装很简单

pip install pymongo

1.连接MongoDB

""" An example of how to connect to MongoDB """
from pymongo import MongoClient
import pymongo.errors as mongoerr
try:
    client = MongoClient('localhost:27017',connect =True)
    db = client['testdb']
    collection = db['users']
    print(collection.find_one())
except mongoerr.ConnectionFailure as  e:
    print( "Could not connect: %s" % e)
finally:
    print('finally run')
    client.close()

2.insert /delete/update

示例:

#2.insert /delete/update
from pymongo import MongoClient
import pymongo.errors as mongoerr
from datetime import datetime
user_doc1 = {
    "username" : "Mary","firstname" : "Mike","surname" : "Doe","dateofbirth" : datetime(1977, 3, 22),
    "email" : "maymike78@example.com", "score" : 50}
user_doc2 = {
    "username" : "Mary","firstname" : "Mike","surname" : "Doe","dateofbirth" : datetime(1977, 3, 22),
    "email" : "maymike78@example.com", "score" : 80}
user_docs = [
    {"username" : "janedoe","firstname" : "Jane","surname" : "Doe","dateofbirth" : datetime(1974, 1, 12),
    "email" : "janedoe74@example.com", "score" : 70},
    {"username" : "Woo","firstname" : "Mary","surname" : "Doe","dateofbirth" : datetime(1998, 4, 12),
    "email" : "woomary98@example.com", "score" : 80},
    {"username" : "lee","firstname" : "Jane","surname" : "Doe","dateofbirth" : datetime(1989, 2, 1),
    "email" : "janelee89@example.com", "score" : 90}]   
#connect
client = MongoClient('localhost:27017',connect =True)
db = client['testdb']
collection = db['users']

#delete all rows
print(collection.delete_many({}))

#insert rows
print(collection.insert_one(user_doc1,bypass_document_validation=True))
print(collection.insert_many(user_docs))

#query and update
print('all rows:',collection.count({}))
print('match username = janedoe rows:',collection.count({"username" : "janedoe"}))
print('Mary data before update:',collection.find_one({"username" : "Mary"}))
collection.find_one_and_replace({"username" : "May"},user_doc2)
print('Mary data after update:',collection.find_one({"username" : "Mary"}))
#update insert
result = collection.update_one({'username': "Mary"}, {'$set':user_doc2},upsert=True)
print('Mary data after insert update rows:',result.matched_count)

#delete all rows
print('Mary data delete:',collection.delete_one({"username" : "Mary"}))

client.close()

结果:

<pymongo.results.DeleteResult object at 0x7fb34805da68>
<pymongo.results.InsertOneResult object at 0x7fb34805da68>
<pymongo.results.InsertManyResult object at 0x7fb3303a70d8>
all rows: 4
match username = janedoe rows: 1
Mary data before update: {'email': 'maymike78@example.com', 'firstname': 'Mike', 'dateofbirth': datetime.datetime(1977, 3, 22, 0, 0), 'surname': 'Doe', 'username': 'Mary', '_id': ObjectId('575fbca099f41d242f25c7fb'), 'score': 50}
Mary data after update: {'email': 'maymike78@example.com', 'firstname': 'Mike', 'dateofbirth': datetime.datetime(1977, 3, 22, 0, 0), 'surname': 'Doe', 'username': 'Mary', '_id': ObjectId('575fbca099f41d242f25c7fb'), 'score': 50}
Mary data delete: <pymongo.results.DeleteResult object at 0x7fb34805da68>

3.find

import pymongo 
client = pymongo.MongoClient('localhost:27017',connect =True)
db = client['testdb']
collection = db['users']

#大于条件
print('match firstname = Jane and score >80 rows:',collection.count({"firstname" : "Jane",'score':{"$gt":80}}))

#top(n),结果排序,限定记录数,限定显示字段
for post in collection.find({ 'firstname': 'Jane'},{"firstname":1,"email":2,"score":3},sort=[("score", pymongo.DESCENDING)]).limit(2).skip(1):
    print('find 2nd row',post)
# or的条件
for post in collection.find({'$or' :[{ 'firstname': 'Jane'},{ 'firstname': 'Mary'}]},{"firstname":1,"email":2,"score":3}):
    print('or select ',post)
#正则表达式
for post in collection.find({ "firstname": {'$regex':'^M'}},{"firstname":1,"email":2,"score":3}):
    print('reg start with J',post)
client.close()

结果:

match firstname = Jane and score >80 rows: 1
find 2nd row {'email': 'janedoe74@example.com', '_id': ObjectId('575fbca099f41d242f25c7fc'), 'firstname': 'Jane', 'score': 70}
or select {'email': 'janedoe74@example.com', '_id': ObjectId('575fbca099f41d242f25c7fc'), 'firstname': 'Jane', 'score': 70}
or select {'email': 'woomary98@example.com', '_id': ObjectId('575fbca099f41d242f25c7fd'), 'firstname': 'Mary', 'score': 80}
or select {'email': 'janelee89@example.com', '_id': ObjectId('575fbca099f41d242f25c7fe'), 'firstname': 'Jane', 'score': 90}
reg start with J {'email': 'woomary98@example.com', '_id': ObjectId('575fbca099f41d242f25c7fd'), 'firstname': 'Mary', 'score': 80}

一些比较条件表达式:


4.create index

from pymongo import MongoClient
client = MongoClient('localhost:27017',connect =True)
db = client['testdb']
collection = db['users']
# Create index on username property called "username_idx"
collection.create_index("firstname", name="firstname_idx")
print('find Mary:',collection.find_one({"firstname" : "Mary"},{"firstname":1,"email":2,"score":3}))
#Delete index called "username_idx"
collection.drop_index("firstname_idx")
client.close()

5.导出到csv

#export to csv
from pymongo import MongoClient
import csv
client = MongoClient('127.0.0.1', 27017)
collection = client.testdb.users
FIELDS = [ 'firstname','email','_id']
csv_file = open('test.csv', 'w')  
dict_writer = csv.DictWriter(csv_file, fieldnames=FIELDS)  
dict_writer.writeheader()
dict_writer.writerows(collection.find({},{"firstname":1,"email":2}))

csv_file.close()
client.close()

6.示例代码(ipynb)

推荐 5
本文由 seng 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

3 个评论

下载代码了,谢谢
今天更新了一版,昨天的版本有些错误。
增加了导出到csv代码

要回复文章请先登录注册