pymongo增删查改以及条件查询
---恢复内容开始---
下载Pymongo
pip install pymongo
pip install pymongo==x.x.x指定下载版本
连接数据库
from pymongo import MongoClient client = MongoClient()#连接默认的主机和端口 client = MongoClient('localhost', 27017)#连接到localhost的27017端口 client = MongoClient('mongodb://localhost:27017/')#使用MongoDB URI模式
db = client.test#连接到test数据库 db = client['test']
collection = db.python_collection#连接到python_collection表 collection = db['python_collection']
增删改查操作
#!/usr/bin/python3 #coding=utf-8 import datetime from pymongo import MongoClient client = MongoClient() db = client.pythondb post = {"author": "Maxsu", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"], "date": datetime.datetime.utcnow()} posts = db.posts post_id = posts.insert_one(post).inserted_id print ("post id is ", post_id)
Insert_one()插入文档
#!/usr/bin/python3 #coding=utf-8 import datetimefrom pymongo import MongoClient client = MongoClient() db = client.pythondb ''' post = {"author": "Maxsu", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"], "date": datetime.datetime.utcnow()} ''' posts = db.posts #post_id = posts.insert_one(post).inserted_id #print ("post id is ", post_id) print(posts.find_one())
# 条件chaxun
result = post.find({'字段名':{'mongodb条件查询的筛选器':{数据}}})
例子
result = post.find({'author':{'$in':['abc', 'def']}})
查询post中author字段等于abc以及def的所有数据
将'$in'替换成其他的条件查询筛选器
find_one()查询单个文档
import datetime import pprint from pymongo import MongoClient client = MongoClient() db = client.pythondb new_posts = [{"_id": 1000, "author": "Curry", "text": "Another post!", "tags": ["bulk", "insert"], "date": datetime.datetime(2017, 11, 12, 11, 14)}, {"_id": 1001,"author": "Maxsu", "title": "MongoDB is fun", "text": "and pretty easy too!", "date": datetime.datetime(2019, 11, 10, 10, 45)}] posts = db.posts result = posts.insert_many(new_posts) print("Bulk Inserts Result is :", result.inserted_ids)
insert_many()插入多个文档
#!/usr/bin/python3 #coding=utf-8 import datetime import pprint from pymongo import MongoClient client = MongoClient() db = client.pythondb posts = db.posts for post in posts.find(): pprint.pprint(post)
find()查询多个文档
count()计数
sort()排序
create_index()创建索引
>>> for doc in db.test.find(): ... print(doc) ... {u'x': 1, u'_id': 0} {u'x': 1, u'_id': 1} {u'x': 1, u'_id': 2} >>> result = db.test.update_one({'x': 1}, {'$inc': {'x': 3}}) >>> result.matched_count 1 >>> result.modified_count 1 >>> for doc in db.test.find(): ... print(doc) ... {u'x': 4, u'_id': 0} {u'x': 1, u'_id': 1} {u'x': 1, u'_id': 2}
update_one
>>> for doc in db.test.find(): ... print(doc) ... {u'x': 1, u'_id': 0} {u'x': 1, u'_id': 1} {u'x': 1, u'_id': 2} >>> result = db.test.update_many({'x': 1}, {'$inc': {'x': 3}}) >>> result.matched_count 3 >>> result.modified_count 3 >>> for doc in db.test.find(): ... print(doc) ... {u'x': 4, u'_id': 0} {u'x': 4, u'_id': 1} {u'x': 4, u'_id': 2}
update_many
>>> db.test.count({'x': 1}) 3 >>> result = db.test.delete_one({'x': 1}) >>> result.deleted_count 1 >>> db.test.count({'x': 1}) 2
delete_one
>>> db.test.count({'x': 1}) 3 >>> result = db.test.delete_many({'x': 1}) >>> result.deleted_count 3 >>> db.test.count({'x': 1}) 0
delete_many
显式创建集合
db.create_collection("test", validator={"name": {"$type": "string"})
如果要验证的类型为int的话,需要使用NumberInt()函数转换要插入的值,否则通过。
更多内容查阅pymongo官方文档