import pymongo
「」"------------------------------準備工作------------------------------------"""
client = pymongo.mongoclient(host=
'localhost'
, port=
27017
)client = pymongo.mongoclient(
'mongodb:localhost:27017'
)
db = client.mongo
db = client[
'mongo'
]
collection = db.students
collections = db[
'students'
]
「」"------------------------------插入資料------------------------------------"""
#插入資料,推薦使用 insert_one 和 insert_many 兩個函式分別用於插入一條資料和多條資料
student =
result = collection.insert_one(student)
print
(result)
student1 =
student2 =
result = collection.insert_many(
[student1, student2]
)print
(result)
「」"------------------------------查詢資料------------------------------------"""
result = collection.find_one(
)print
(type
(result)
, result)
results = collection.find(
)for r in results:
print
(r)
"""
$lt 小於 }
$gt 大於 }
$lte 小於等於 }
$gte 大於等於 }
$ne 不等於 }
$in 在範圍內 }
$nin 不在範圍內 }
"""print
(collection.find_one(})
)
print
(collection.find_one(})
)
「」"------------------------------更新資料------------------------------------"""
condition =
student = collection.find_one(condition)
student[
'age']=
26result = collection.update_one(condition,
)print
(result)
print
(result.matched_count, result.modified_count)
result = collection.update_one(condition,})
print
(result)
print
(result.matched_count, result.modified_count)
result = collection.update_many(condition,})
print
(result)
print
(result.matched_count, result.modified_count)
「」"------------------------------刪除資料------------------------------------"""
result = collection.delete_one(
)print
(result)
print
(result.deleted_count)
result = collection.delete_many(
)print
(result)
print
(result.deleted_count)
result = collection.delete_one(})
print
(result)
print
(result.deleted_count)
pymongo 基本用法 匯入匯出
import numpy as np import pandas as pd import matplotlib.pyplot as plt import os import re import warnings import time warnings.filterwarnings ignore ...
PyMongo基本使用
引用pymongo import pymongo 建立連線connection import pymongo conn pymongo.connection localhost 27017 或import conn connection localhost 27017 建立connection時,指...
PyMongo基本使用
引用pymongo import pymongo 建立連線connection import pymongo conn pymongo.connection localhost 27017 或 from pymongo import connection conn connection localh...