def find_colum3(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) for y in col.find({}, {"name": 1, "alexa": 0}): print(y)
def insert_many(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) l_list = [{ "name": "Taobao", "alexa": "100", "url": "https://www.taobao.com" }, { "name": "QQ", "alexa": "101", "url": "https://www.qq.com" }, { "name": "Facebook", "alexa": "10", "url": "https://www.facebook.com" }, { "name": "知乎", "alexa": "103", "url": "https://www.zhihu.com" }, { "name": "Github", "alexa": "109", "url": "https://www.github.com" }] # 集合中插入文档 x = col.insert_many(l_list) print(x) # 输出插入的所有文档对应的 _id 值 print(x.inserted_ids)
def insert_many2(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) l_list = [{ "_id": 1, "name": "RUNOOB", "cn_name": "菜鸟教程" }, { "_id": 2, "name": "Google", "address": "Google 搜索" }, { "_id": 3, "name": "Facebook", "address": "脸书" }, { "_id": 4, "name": "Taobao", "address": "淘宝" }, { "_id": 5, "name": "Zhihu", "address": "知乎" }] # 集合中插入文档 x = col.insert_many(l_list) print(x) # 输出插入的所有文档对应的 _id 值 print(x.inserted_ids)
def drop(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) query = {} col.drop(query)
def sort_desc(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) a = col.find().sort("alexa", -1) for x in a: print(x)
def find_query(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) query = {"name": "RUNOOB"} for y in col.find(query): print(y)
def find_one(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) # 查询一条数据 x = col.find_one() print(x)
def find_limit(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) result = col.find().limit(3) for y in result: print(y)
def find_regular(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) query = {"name": {"$regex": "^R"}} for y in col.find(query): print(y)
def find(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) query = {"alexa": "12345"} for y in col.find(query): print(y)
def find(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) # 查询全量数据 x = col.find() for y in x: print(y)
def delete_many_all(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) query = {} x = col.delete_many(query) # 删除后输出 print(x.deleted_count, "个文档已删除")
def delete_one(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) query = {"name": "Taobao"} col.delete_one(query) # 删除后输出 for x in col.find(): print(x)
def update_many(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) query = {"alexa": "12345"} newvalues = {"$set": {"alexa": "10000"}} col.update_many(query, newvalues) # 输出修改后的 "sites" 集合 for x in col.find(query): print(x)
def insert_one(): with DB(host=h, port=pr, database=d, user=u, passwd=pw) as db: # 指定集合 col = db["sites"] print(db) d_dict = { "name": "RUNOOB", "alexa": "10000", "url": "https://www.runoob.com" } # 集合中插入文档 x = col.insert_one(d_dict) print(x)