搞懂Python mongodb的基本操作
$ python -m pip3 install pymongo
$ python -m pip3 install pymongo==3.5.1
$ python -m pip3 install --upgrade pymongo
$ python -m easy_install pymongo
$ python -m easy_install -U pymongo
import pymongo
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
dblist = myclient.list_database_names()
# dblist = myclient.database_names()
if "testdb" in dblist:
print("数据库已存在!")
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['testdb']
collist = mydb. list_collection_names()
# collist = mydb.collection_names()
# 判断 sites 集合是否存在
if "sites" in collist:
print("集合已存在!")
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
mydict = { "name": "Lidihuo", "url": "https://www.lidihuo.com" }
x = mycol.insert_one(mydict)
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['testdb']
mycol = mydb["sites"]
mydict = { "name": "Google", "url": "https://www.google.com" }
x = mycol.insert_one(mydict)
print(x.inserted_id)
5f17ddeaf99e553262c65b88
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
mylist = [
{ "name": "Taobao","url": "https://www.taobao.com" },
{ "name": "QQ", "url": "https://www.qq.com" },
{ "name": "Facebook", "url": "https://www.facebook.com" },
{ "name": "知乎","url": "https://www.zhihu.com" },
{ "name": "Github","url": "https://www.github.com" }
]
x = mycol.insert_many(mylist)
# 输出插入的所有文档对应的 _id 值
print(x.inserted_ids)
[ObjectId('5f17de561d5e8966224d5734'), ObjectId('5f17de561d5e8966224d5735'), ObjectId('5f17de561d5e8966224d5736'), ObjectId('5f17de561d5e8966224d5737'), ObjectId('5f17de561d5e8966224d5738')]
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["site2"]
mylist = [
{ "_id": 1, "name": "Lidihuo", "name": "立地货"},
{ "_id": 2, "name": "Google", "name": "Google 搜索"},
{ "_id": 3, "name": "Facebook", "name": "脸书"},
{ "_id": 4, "name": "Taobao", "name": "淘宝"},
{ "_id": 5, "name": "Zhihu", "name": "知乎"}
]
x = mycol.insert_many(mylist)
# 输出插入的所有文档对应的 _id 值
print(x.inserted_ids)
[1, 2, 3, 4, 5]
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
x = mycol.find_one()
print(x)
{'_id': ObjectId('5f17ddd0b141d312e5c38e07'), 'name': 'Lidihuo', 'url': 'https://www.lidihuo.com'}
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
for x in mycol.find():
print(x)
{'_id': ObjectId('5f17ddd0b141d312e5c38e07'), 'name': 'Lidihuo', 'url': 'https://www.lidihuo.com'}
{'_id': ObjectId('5f17ddeaf99e553262c65b88'), 'name': 'Google', 'url': 'https://www.google.com'}
{'_id': ObjectId('5f17de561d5e8966224d5734'), 'name': 'Taobao', 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5f17de561d5e8966224d5735'), 'name': 'QQ', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5f17de561d5e8966224d5736'), 'name': 'Facebook', 'url': 'https://www.facebook.com'}
{'_id': ObjectId('5f17de561d5e8966224d5737'), 'name': '知乎', 'url': 'https://www.zhihu.com'}
{'_id': ObjectId('5f17de561d5e8966224d5738'), 'name': 'Github', 'url': 'https://www.github.com'}
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
for x in mycol.find({},{ "_id": 0, "name": 1}):
print(x)
{'name': 'Lidihuo'}
{'name': 'Google'}
{'name': 'Taobao'}
{'name': 'QQ'}
{'name': 'Facebook'}
{'name': '知乎'}
{'name': 'Github'}
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
for x in mycol.find({},{ "name": 0 }):
print(x)
{'_id': ObjectId('5f17ddd0b141d312e5c38e07'), 'url': 'https://www.lidihuo.com'}
{'_id': ObjectId('5f17ddeaf99e553262c65b88'), 'url': 'https://www.google.com'}
{'_id': ObjectId('5f17de561d5e8966224d5734'), 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5f17de561d5e8966224d5735'), 'url': 'https://www.qq.com'}
{'_id': ObjectId('5f17de561d5e8966224d5736'), 'url': 'https://www.facebook.com'}
{'_id': ObjectId('5f17de561d5e8966224d5737'), 'url': 'https://www.zhihu.com'}
{'_id': ObjectId('5f17de561d5e8966224d5738'), 'url': 'https://www.github.com'}
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
myquery = { "name": "Lidihuo" }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
{'_id': ObjectId('5f17ddd0b141d312e5c38e07'), 'name': 'Lidihuo', 'url': 'https://www.lidihuo.com'}
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
myquery = { "name": { "$gt": "L" } }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
myquery = { "name": { "$regex": "^L" } }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
{'_id': ObjectId('5f17ddd0b141d312e5c38e07'), 'name': 'Lidihuo', 'url': 'https://www.lidihuo.com'}
{'_id': ObjectId('5f17de561d5e8966224d5734'), 'name': 'Taobao', 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5f17de561d5e8966224d5735'), 'name': 'QQ', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5f17de561d5e8966224d5737'), 'name': '知乎', 'url': 'https://www.zhihu.com'}
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
myresult = mycol.find().limit(3)
# 输出结果
for x in myresult:
print(x)
{'_id': ObjectId('5f17ddd0b141d312e5c38e07'), 'name': 'Lidihuo', 'url': 'https://www.lidihuo.com'}
{'_id': ObjectId('5f17ddeaf99e553262c65b88'), 'name': 'Google', 'url': 'https://www.google.com'}
{'_id': ObjectId('5f17de561d5e8966224d5734'), 'name': 'Taobao', 'url': 'https://www.taobao.com'}
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
myquery = { "name": "Lidihuo" }
newvalues = { "$set": { "name": "LDH" } }
mycol.update_one(myquery, newvalues)
# 输出修改后的 "sites" 集合
for x in mycol.find():
print(x)
{'_id': ObjectId('5f17ddd0b141d312e5c38e07'), 'name': 'LDH', 'url': 'https://www.lidihuo.com'}
{'_id': ObjectId('5f17ddeaf99e553262c65b88'), 'name': 'Google', 'url': 'https://www.google.com'}
{'_id': ObjectId('5f17de561d5e8966224d5734'), 'name': 'Taobao', 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5f17de561d5e8966224d5735'), 'name': 'QQ', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5f17de561d5e8966224d5736'), 'name': 'Facebook', 'url': 'https://www.facebook.com'}
{'_id': ObjectId('5f17de561d5e8966224d5737'), 'name': '知乎', 'url': 'https://www.zhihu.com'}
{'_id': ObjectId('5f17de561d5e8966224d5738'), 'name': 'Github', 'url': 'https://www.github.com'}
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
myquery = { "name": { "$regex": "^L" } }
newvalues = { "$set": { "name": "Lidihuo" } }
x = mycol.update_many(myquery, newvalues)
print(x.modified_count, "文档已修改")
n class="brown">for x
in mycol.
find():
print(x)
1 文档已修改
{'_id': ObjectId('5f17ddd0b141d312e5c38e07'), 'name': 'Lidihuo', 'url': 'https://www.lidihuo.com'}
{'_id': ObjectId('5f17ddeaf99e553262c65b88'), 'name': 'Google', 'url': 'https://www.google.com'}
{'_id': ObjectId('5f17de561d5e8966224d5734'), 'name': 'Taobao', 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5f17de561d5e8966224d5735'), 'name': 'QQ', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5f17de561d5e8966224d5736'), 'name': 'Facebook', 'url': 'https://www.facebook.com'}
{'_id': ObjectId('5f17de561d5e8966224d5737'), 'name': '知乎', 'url': 'https://www.zhihu.com'}
{'_id': ObjectId('5f17de561d5e8966224d5738'), 'name': 'Github', 'url': 'https://www.github.com'}
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
mydoc = mycol.find().sort("name")
for x in mydoc:
print(x)
{'_id': ObjectId('5f17de561d5e8966224d5736'), 'name': 'Facebook', 'url': 'https://www.facebook.com'}
{'_id': ObjectId('5f17de561d5e8966224d5738'), 'name': 'Github', 'url': 'https://www.github.com'}
{'_id': ObjectId('5f17ddeaf99e553262c65b88'), 'name': 'Google', 'url': 'https://www.google.com'}
{'_id': ObjectId('5f17ddd0b141d312e5c38e07'), 'name': 'Lidihuo', 'url': 'https://www.lidihuo.com'}
{'_id': ObjectId('5f17de561d5e8966224d5735'), 'name': 'QQ', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5f17de561d5e8966224d5734'), 'name': 'Taobao', 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5f17de561d5e8966224d5737'), 'name': '知乎', 'url': 'https://www.zhihu.com'}
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
mydoc = mycol.find().sort("name", -1)
for x in mydoc:
print(x)
{'_id': ObjectId('5f17de561d5e8966224d5737'), 'name': '知乎', 'url': 'https://www.zhihu.com'} {'_id': ObjectId('5f17de561d5e8966224d5734'), 'name': 'Taobao', 'url': 'https://www.taobao.com'} {'_id': ObjectId('5f17de561d5e8966224d5735'), 'name': 'QQ', 'url': 'https://www.qq.com'} {'_id': ObjectId('5f17ddd0b141d312e5c38e07'), 'name': 'Lidihuo', 'url': 'https://www.lidihuo.com'} {'_id': ObjectId('5f17ddeaf99e553262c65b88'), 'name': 'Google', 'url': 'https://www.google.com'} {'_id': ObjectId('5f17de561d5e8966224d5738'), 'name': 'Github', 'url': 'https://www.github.com'} {'_id': ObjectId('5f17de561d5e8966224d5736'), 'name': 'Facebook', 'url': 'https://www.facebook.com'}
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
myquery = { "name": "Taobao" }
mycol.delete_one(myquery)
# 删除后输出
for x in mycol.find():
print(x)
{'_id': ObjectId('5f17ddd0b141d312e5c38e07'), 'name': 'Lidihuo', 'url': 'https://www.lidihuo.com'}
{'_id': ObjectId('5f17ddeaf99e553262c65b88'), 'name': 'Google', 'url': 'https://www.google.com'}
{'_id': ObjectId('5f17de561d5e8966224d5735'), 'name': 'QQ', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5f17de561d5e8966224d5736'), 'name': 'Facebook', 'url': 'https://www.facebook.com'}
{'_id': ObjectId('5f17de561d5e8966224d5737'), 'name': '知乎', 'url': 'https://www.zhihu.com'}
{'_id': ObjectId('5f17de561d5e8966224d5738'), 'name': 'Github', 'url': 'https://www.github.com'}
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
myquery = { "name": {"$regex": "^F"} }
x = mycol.delete_many(myquery)
print(x.deleted_count, "个文档已删除")
1 个文档已删除
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
x = mycol.delete_many({})
print(x.deleted_count, "个文档已删除")
5 个文档已删除
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["testdb"]
mycol = mydb["sites"]
mycol.drop()