最近在工作中使用到了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)