Example #1
0
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)
Example #2
0
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)
Example #3
0
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)
Example #4
0
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)
Example #5
0
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)
Example #6
0
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)
Example #7
0
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)
Example #8
0
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)
Example #9
0
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)
Example #10
0
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)
Example #11
0
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)
Example #12
0
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, "个文档已删除")
Example #13
0
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)
Example #14
0
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)
Example #15
0
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)