1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| from pymongo import MongoClient
clinet = MongoClient("mongodb://localhost:27017")
db = clinet["demo"] col = db["demo"]
print(">>> 查看数据库信息") server_info = clinet.server_info() print(server_info)
print(">>> 显示当前数据库服务器上的数据库名") database_names = clinet.list_database_names() print(database_names)
print(">>> 显示当前数据库上的全部集合名") collection_names = db.list_collection_names() print(collection_names)
print(">>> 插入一个文档") demo = { "author": "Sitoi", "age": 22, "title": "Sitoi-blog", "tags": ["man", "spider"] } demo_id = col.insert_one(demo).inserted_id print(demo_id)
print(">>> 插入多个文档") demos = [ {"author": "blog", "age": 18, "title": "blog", "text": "Sitoi Blog"}, {"author": "bash", "age": 30, "title": "bash", "text": "Sitoi Blog"}, {"author": "python", "age": 50, "title": "language", "text": "Sitoi Blog"}, {"author": "mongodb", "age": 80, "title": "NoSQL", "text": "Sitoi Blog"}, {"author": "pymongo", "age": 97, "title": "Python for MongoDB", "text": "Sitoi Blog"}, ] demo_ids = col.insert_many(demos).inserted_ids print(demo_ids)
print(">>> 查询单个文档") query = {} projection = None result = col.find_one(filter=query, projection=projection) print(result)
print(">>> 查询多个文档") query = {} projection = None result = col.find(filter=query, projection=projection) for one in result: print(one)
print(">>> 所有字段")
results = col.find() for one in results: print(one)
print(">>> 用字典指定要显示的哪几个字段") query = {} projection = {"_id": True, "author": True} results = col.find(filter=query, projection=projection) for one in results: print(one)
print(">>> 用字典指定去掉哪些字段") query = {} projection = {"_id": False, "author": False} results = col.find(query, projection=projection) for one in results: print(one)
print(">>> 用列表指定要显示哪几个字段") query = {} projection = ["author", "title"] results = col.find(filter=query, projection=projection) for one in results: print(one)
print(">>> 指定范围,大于等于,小于等于") query = {"age": {"$gte": 10, "$lte": 30}} projection = None results = col.find(filter=query, projection=projection) for one in results: print(one)
print(">>> 不同字段,并列条件") query = {"author": "Sitoi", "age": 22} projection = None results = col.find(filter=query, projection=projection) for one in results: print(one)
print(">>> 相同字段,并列条件")
query = {"age": {"$gt": 50}, "age": {"$lt": 100}}
query = {"age": {"$gte": 10, "$lte": 20}} projection = None results = col.find(filter=query, projection=projection) for one in results: print(one)
print(">>> 不同字段,或条件") query = {"$or": [{"age": 22}, {"author": "blog"}]} projection = None results = col.find(filter=query, projection=projection) for one in results: print(one)
print(">>> 相同字段,或条件") query = {"$or": [{"age": 22}, {"age": 18}]} projection = None results = col.find(filter=query, projection=projection) for one in results: print(one)
print(">>> 字段不存在") query = {"text": None} projection = None results = col.find(filter=query, projection=projection) for one in results: print(one)
print(">>> 字段存在") query = {"text": {"$ne": None}} projection = None results = col.find(filter=query, projection=projection) for one in results: print(one)
print(">>> 正则查询") query = {"author": {"$regex": "^S.*"}} projection = None results = col.find(filter=query, projection=projection) for one in results: print(one)
print(">>> 计数") query = {} count = col.count_documents(filter=query) print(count)
print(">>> 排序") results = col.find().sort([("author", 1), ("title", -1)]) for one in results: print(one)
print(">>> 跳过") results = col.find().skip(1) for one in results: print(one)
print(">>> 限制") results = col.find().limit(1) for one in results: print(one)
print(">>> 更新单个文档") query = {"age": 18} update = {"$set": {"age": 20}} modified_count = col.update_one(filter=query, update=update, upsert=False).modified_count print(modified_count)
print(">>> 更新多个文档") query = {"text": "Sitoi Blog"} update = {"$set": {"text": "Sitoi PyMongo"}} modified_count = col.update_many(filter=query, update=update, upsert=False).modified_count print(modified_count)
print(">>> 删除单个文档") query = {"author": "Sitoi"} delete_count = col.delete_one(filter=query).deleted_count print(delete_count)
print(">>> 删除多个文档") query = {} delete_count = col.delete_many(filter=query).deleted_count print(delete_count)
|