mongo-pymongo 数组操作
2016-09-03 11:38
197 查看
因为刚刚开始使用mongodb时时间比较急,没有比较系统的去学习文档,所以当时在对数组操作时都是自己写代码去操作,所以专门把数组操作单独记录下来
https://docs.mongodb.com/manual/tutorial/query-documents/#read-operations-arrays
插入测试数据
普通匹配,数组必须从内容及顺序完全相同才可以匹配
包含匹配
位置匹配
元素的单个子元素多重条件判断
元素的多个子元素多重条件判断
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 } ] } ] )
普通匹配,数组必须从内容及顺序完全相同才可以匹配
def find_array(): d = mongo_db['users'].find( filter={ "badges": [ "black", "blue" ] }, ) for a in d: print(a)
包含匹配
def find_array(): d = mongo_db['users'].find( filter={ "badges": "black" #获取 所有数组badges中包含了black的doc }, ) for a in d: print(a)
位置匹配
def find_array(): d = mongo_db['users'].find( filter={ "badges.0": "black" }, ) for a in d: print(a)
元素的单个子元素多重条件判断
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)
元素的多个子元素多重条件判断
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)
相关文章推荐
- mongo-pymongo 数组操作
- mongo操作数组字段
- python mongo数据库出错解决及pymongo操作
- segmentfault.com mongo出识以及对数组的操作
- mongo对内嵌数组的操作
- cobol数组操作
- COBOL中数组操作
- 关于php中的数组操作
- asp编写用数组操作数据库记录的函数
- JAVA操作数组的异常
- 关于arrylist的一些操作(包含利用params传递数组参数)
- Java学习,数组的相关操作,封装类,Class类,反射API
- 巧用标准c++中的算法函数,对数组进行操作
- 浅谈C#中的数组类System.Array 操作
- C++ 操作数组的类
- 对数组的操作
- JAVA数组和JAV集合类的操作
- C语言中文件操作--如何将文本数据正确读到字符串数组中
- 基于php常用函数总结(数组,字符串,时间,文件操作)
- COBOL中数组操作