查看数据库
from pymongo import MongoClient
connect=MongoClient(host='localhost', port=27017, username="root", password="123456")
connect=MongoClient('mongodb://localhost:27017/', username="root", password="123456")
print(connect.list_database_names())
获取二手游戏账号转让平台数据库实例
test_db=connect['test']
获取collection实例
collection=test_db['students']
插入一行document, 查询一行document,取出一行document的值
from pymongo import MongoClient
from datetime import datetime
connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)
# 获取db
test_db=connect['test']
# 获取collection
collection=test_db['students']
# 构建document
document={"author": "Mike", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"], "date": datetime.now()}
# 插入document
one_insert=collection.insert_one(document=document)
print(one_insert.inserted_id)
# 通过条件过滤出一条document
one_result=collection.find_one({"author": "Mike"})
# 解析document字段
print(one_result, type(one_result))
print(one_result['_id'])
print(one_result['author'])
注意:如果需要通过id查询一行document,需要将id包装为ObjectId类的实例对象
from bson.objectid import ObjectId
collection.find_one({'_id': ObjectId('5c2b18dedea5818bbd73b94c')})
插入多行documents, 查询多行document, 查看collections有多少行document
from pymongo import MongoClient
from datetime import datetime
connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)
# 获取db
test_db=connect['test']
# 获取collection
collection=test_db['students']
documents=[{"author": "Mike","text": "Another post!","tags": ["bulk", "insert"], "date": datetime(2009, 11, 12, 11, 14)},
{"author": "Eliot", "title": "MongoDB is fun", "text": "and pretty easy too!", "date": datetime(2009, 11, 10, 10, 45)}]
collection.insert_many(documents=documents)
# 通过条件过滤出多条document
documents=collection.find({"author": "Mike"})
# 解析document字段
print(documents, type(documents))
print(''300)
for document in documents:
print(document)
print(''300)
result=collection.count_documents({'author': 'Mike'})
print(result)
范围比较查询
from pymongo import MongoClient
from datetime import datetime
connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)
# 获取db
test_db=connect['test']
# 获取collection
collection=test_db['students']
# 通过条件过滤时间小于datetime(2021, 1,1,15,40,3) 的document
documents=collection.find({"date": {"$lt": datetime(2021, 1,1,15,40,3)}}).sort('date')
# 解析document字段
print(documents, type(documents))
print(''300)
for document in documents:
print(document)
创建索引
from pymongo import MongoClient
import pymongo
from datetime import datetime
connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)
# 获取db
test_db=connect['test']
# 获取collection
collection=test_db['students']
# 创建字段索引
collection.create_index(keys=[("name", pymongo.DESCENDING)], unique=True)
# 查询索引
result=sorted(list(collection.index_information()))
print(result)
document修改
from pymongo import MongoClient
connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)
# 获取db
test_db=connect['test']
# 获取collection
collection=test_db['students']
result=collection.update({'name': 'robby'}, {'$set': {"name": "Petter"}})
print(result)
注意:还有update_many()方法
document删除
from pymongo import MongoClient
connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)
# 获取db
test_db=connect['test']
# 获取collection
collection=test_db['students']
result=collection.delete_one({'name': 'Petter'})
print(result.deleted_count)
注意:还有delete_many()方法