mongo-pymongo 數組操作

因爲剛剛開始使用mongodb時時間比較急,沒有比較系統的去學習文檔,所以當時在對數組操作時都是自己寫代碼去操作,所以專門把數組操作單獨記錄下來
https://docs.mongodb.com/manual/tutorial/query-documents/#read-operations-arrays
插入測試數據

db.users.insert(
  [
     {
       _id: 1,
       name: "sue",
       age: 19,
       type: 1,
       status: "P",
       favorites: { artist: "Picasso", food: "pizza" },
       finished: [ 17, 3 ],
       badges: [ "blue", "black" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 85, bonus: 10 }
       ]
     },
     {
       _id: 2,
       name: "bob",
       age: 42,
       type: 1,
       status: "A",
       favorites: { artist: "Miro", food: "meringue" },
       finished: [ 11, 25 ],
       badges: [ "green" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 64, bonus: 12 }
       ]
     },
     {
       _id: 3,
       name: "ahn",
       age: 22,
       type: 2,
       status: "A",
       favorites: { artist: "Cassatt", food: "cake" },
       finished: [ 6 ],
       badges: [ "blue", "red" ],
       points: [
          { points: 81, bonus: 8 },
          { points: 55, bonus: 20 }
       ]
     },
     {
       _id: 4,
       name: "xi",
       age: 34,
       type: 2,
       status: "D",
       favorites: { artist: "Chagall", food: "chocolate" },
       finished: [ 5, 11 ],
       badges: [ "red", "black" ],
       points: [
          { points: 53, bonus: 15 },
          { points: 51, bonus: 15 }
       ]
     },
     {
       _id: 5,
       name: "xyz",
       age: 23,
       type: 2,
       status: "D",
       favorites: { artist: "Noguchi", food: "nougat" },
       finished: [ 14, 6 ],
       badges: [ "orange" ],
       points: [
          { points: 71, bonus: 20 }
       ]
     },
     {
       _id: 6,
       name: "abc",
       age: 43,
       type: 1,
       status: "A",
       favorites: { food: "pizza", artist: "Picasso" },
       finished: [ 18, 12 ],
       badges: [ "black", "blue" ],
       points: [
          { points: 78, bonus: 8 },
          { points: 57, bonus: 7 }
       ]
     }
  ]
)
  1. 普通匹配,數組必須從內容及順序完全相同纔可以匹配
def find_array():
    d = mongo_db['users'].find(
        filter={
            "badges": [ "black", "blue" ]
        },
    )
    for a in d:
        print(a)
  1. 包含匹配
def find_array():
    d = mongo_db['users'].find(
        filter={
            "badges": "black"   #獲取 所有數組badges中包含了black的doc
        },
    )
    for a in d:
        print(a)
  1. 位置匹配
def find_array():
    d = mongo_db['users'].find(
        filter={
            "badges.0": "black"
        },
    )
    for a in d:
        print(a)
  1. 元素的單個子元素多重條件判斷
def find_array():
    d = mongo_db['users'].find(
        filter={
            "finished": {"$elemMatch": {'$gt': 15, '$lt': 20}}   # 任意元素匹配所有條件
        },
        #projection={
        #    'finished': 1
        #}
    )
    for a in d:
        print(a)
def find_array():
    d = mongo_db['users'].find(
        filter={
            "finished": {'$gt': 15, '$lt': 20}  # 任意元素匹配其中任意一個條件,不明白這樣的道理,在非數組中兩個條件是and
        },
        projection={
            'finished': 1
        }
    )
    for a in d:
        print(a)
def find_array():
    d = mongo_db['users'].find(
        filter={
            "points.0.points": {'$gt': 50, '$lt': 80}   #第一個元素同時滿足兩個條件
        },
        projection={
            'points': 1
        }
    )
    for a in d:
        print(a)
def find_array():
    d = mongo_db['users'].find(
        filter={
            "points.points": {'$gt': 50, '$lt': 80}   # 任意元素同時滿足此條件
        },
        projection={
            'points': 1
        }
    )
    for a in d:
        print(a)
  1. 元素的多個子元素多重條件判斷
def find_array():
    d = mongo_db['users'].find(
        filter={
            "points": {'$elemMatch': {'points': {'$lte': 70}, 'bonus': 20}}   # 任意元素的子元素同時滿足條件
        },
        projection={
            'points': 1
        }
    )
    for a in d:
        print(a)
def find_array():
    d = mongo_db['users'].find(
        filter={
            "points.points": {'$lte': 70}, "points.bonus": 20   # 所有元素中有可以同時滿足條件的子項
        },
        projection={
            'points': 1
        }
    )
    for a in d:
        print(a)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章