mongodb find語法記錄

collection users 原始數據

{ 
    "_id" : 1.0, 
    "name" : "sue", 
    "age" : 19.0, 
    "type" : 1.0, 
    "status" : "P", 
    "favorites" : {
        "artist" : "Picasso", 
        "food" : "pizza"
    }, 
    "finished" : [
        17.0, 
        3.0
    ], 
    "badges" : [
        "blue", 
        "black"
    ], 
    "points" : [
        {
            "points" : 85.0, 
            "bonus" : 20.0
        }, 
        {
            "points" : 85.0, 
            "bonus" : 10.0
        }
    ], 
    "scholarship" : "First-class", 
    "scholar" : 20000.0, 
    "address" : {
        "province" : "hunan", 
        "city" : "cs"
    }
}
{ 
    "_id" : 3.0, 
    "name" : "ahn", 
    "age" : 22.0, 
    "type" : 2.0, 
    "status" : "A", 
    "favorites" : {
        "artist" : "Cassatt", 
        "food" : "cake"
    }, 
    "finished" : [
        6.0
    ], 
    "badges" : [
        "blue", 
        "red"
    ], 
    "points" : [
        {
            "points" : 81.0, 
            "bonus" : 8.0
        }, 
        {
            "points" : 55.0, 
            "bonus" : 20.0
        }
    ], 
    "scholarship" : "from-david", 
    "scholar" : 20000.0, 
    "address" : {
        "province" : "hunan", 
        "city" : "cs"
    }
}
{ 
    "_id" : 4.0, 
    "name" : "xi", 
    "age" : 34.0, 
    "type" : 2.0, 
    "status" : "D", 
    "favorites" : {
        "artist" : "Chagall", 
        "food" : "chocolate"
    }, 
    "finished" : [
        5.0, 
        11.0
    ], 
    "badges" : [
        "red", 
        "black"
    ], 
    "points" : [
        {
            "points" : 53.0, 
            "bonus" : 15.0
        }, 
        {
            "points" : 51.0, 
            "bonus" : 15.0
        }
    ], 
    "scholarship" : "from-chalie", 
    "scholar" : 20000.0, 
    "address" : {
        "province" : "hunan", 
        "city" : "cs"
    }
}
{ 
    "_id" : 5.0, 
    "name" : "xyz", 
    "age" : 23.0, 
    "type" : 2.0, 
    "status" : "D", 
    "favorites" : {
        "artist" : "Noguchi", 
        "food" : "nougat"
    }, 
    "finished" : [
        14.0, 
        6.0
    ], 
    "badges" : [
        "orange"
    ], 
    "points" : [
        {
            "points" : 71.0, 
            "bonus" : 20.0
        }
    ], 
    "scholarship" : "from-min", 
    "scholar" : 99999.0, 
    "address" : {
        "province" : "hunan", 
        "city" : "cs"
    }
}
{ 
    "_id" : 6.0, 
    "name" : "abc", 
    "age" : 43.0, 
    "type" : 1.0, 
    "status" : "A", 
    "favorites" : {
        "food" : "pizza", 
        "artist" : "Picasso"
    }, 
    "finished" : [
        18.0, 
        12.0
    ], 
    "badges" : [
        "black", 
        "blue"
    ], 
    "points" : [
        {
            "points" : 78.0, 
            "bonus" : 8.0
        }, 
        {
            "points" : 57.0, 
            "bonus" : 7.0
        }
    ], 
    "scholarship" : "from-kkdk", 
    "scholar" : 12000.0, 
    "address" : {
        "province" : "hunan", 
        "city" : "cs"
    }
}
  1. find
def find():
    d = mongo_db['users'].find(
# filter 是查詢的過濾器,結果會符合filter的查詢條件
filter={
# 'name': 'David li',     # 全匹配
'address.province': 'hunan',             # 內層結構全匹配
'address.city': {'$in': ['cs', 'yy']},   # 城市必須是cs, yy的
'scholarship': {'$exists': True},   # 是否存在
'scholar': {'$gt': 14000, '$lt': 80000},
'$or': [{'name': {'$regex': 'ah.*'}}, {'name': 'sue'}],
},

# projection是對列的過濾,結果只包括存在的列
projection={
'name': 1,
'address.city': 1,
'scholar': 1,
'_id': 0
},

skip=0,  # 成第0個找到的項開始
limit=20,  # 獲取20個數據
        # sort={'scholar': 1}
)
'''
            其他比較符 nin, eq, ne, lt, lte, gte,

    '''
d.sort('name', -1)  # -1代表倒序,1升序;因爲Python的語法習慣,會與mongodb的腳本有一定區別,再閱讀文檔時要注意
for a in d:
print(a)

輸出:

{'address': {'city': 'cs'}, 'scholar': 20000.0, 'name': 'sue'}
{'address': {'city': 'cs'}, 'scholar': 20000.0, 'name': 'ahn'}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章