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)

你可能感兴趣的:(Python,mongodb,数据库)