Pythonmongo数据库过滤条件filter的使⽤
____tz_zs
以下为⽤于测试的数据库数据:
{"_id": "2019-01-01T00:00:00Z", "key1": {"internal_key1": {"a": "va", "b": "vb"}}, "key2": "v2"},
{"_id": "2019-01-01T12:00:00Z", "key1": {"internal_key1": {"a": "va", "c": "vc"}, "internal_key2": {}, "internal_key3": "internal_v3"}},
{"_id": "2019-01-02T00:00:00Z", "key1": {"internal_key1": {"a": None, "b": None, "c": None}, "internal_key2": None, "internal_key3": None}, "key2": None}, {"_id": "2019-01-02T12:00:00Z", "key1": {"internal_key1": {"a": {}, "b": {}, "c": {}}, "internal_key2": {"t": 1}, "internal_key3": "internal_v3"}, "key2": {}},
{"_id": "2019-01-03T00:00:00Z", "key1": {"internal_key1": {"a": "va", "b": "vb", "c": "vc"}, "internal_key2": {"t": 1}, "internal_key3": "internal_v3"}, "key2": "v2"}, $exists
根据取对应的键值对是否存在作为判定依据,注意,值为 None 也代表这个字段存在。
find_records = db_collection.find(filter={"key2":{"$exists":True}})
"""
{'_id': '2019-01-01T00:00:00Z', 'key1': {'internal_key1': {'b': 'vb', 'a': 'va'}}, 'key2': 'v2'}
{'_id': '2019-01-02T00:00:00Z', 'key1': {'internal_key1': {'b': None, 'a': None, 'c': None}, 'internal_key3': None, 'internal_key2': None}, 'key2': None}
{'_id': '2019-01-02T12:00:00Z', 'key1': {'internal_key1': {'b': {}, 'a': {}, 'c': {}}, 'internal_key3': 'internal_v3', 'internal_key2': {'t': 1}}, 'key2': {}}
{'_id': '2019-01-03T00:00:00Z', 'key1': {'internal_key1': {'b': 'vb', 'a': 'va', 'c': 'vc'}, 'internal_key3': 'internal_v3', 'internal_key2': {'t': 1}}, 'key2': 'v2'}
"""
多级字典使⽤ “.” 表⽰层级归属
数据库恢复find_records = db_collection.find(filter={"key1.internal_key3":{"$exists":True}})
"""
{'key1': {'internal_key3': 'internal_v3', 'internal_key1': {'c': 'vc', 'a': 'va'}, 'internal_key2': {}}, '_id': '2019-01-01T12:00:00Z'}
{'key1': {'internal_key3': None, 'internal_key1': {'c': None, 'b': None, 'a': None}, 'internal_key2': None}, '_id': '2019-01-02T00:00:00Z', 'key2': None}
{'key1': {'internal_key3': 'internal_v3', 'internal_key1': {'c': {}, 'b': {}, 'a': {}}, 'internal_key2': {'t': 1}}, '_id': '2019-01-02T12:00:00Z', 'key2': {}}
{'key1': {'internal_key3': 'internal_v3', 'internal_key1': {'c': 'vc', 'b': 'vb', 'a': 'va'}, 'internal_key2': {'t': 1}}, '_id': '2019-01-03T00:00:00Z', 'key2': 'v2'}
"""
find_records = db_collection.find(filter={"key1.internal_key2":{"$exists":True}})
"""
{'key1': {'internal_key1': {'a': 'va', 'c': 'vc'}, 'internal_key3': 'internal_v3', 'internal_key2': {}}, '_id': '2019-01-01T12:00:00Z'}
{'key1': {'internal_key1': {'a': None, 'c': None, 'b': None}, 'internal_key3': None, 'internal_key2': None}, '_id': '2019-01-02T00:00:00Z', 'key2': None}
{'key1': {'internal_key1': {'a': {}, 'c': {}, 'b': {}}, 'internal_key3': 'internal_v3', 'internal_key2': {'t': 1}}, '_id': '2019-01-02T12:00:00Z', 'key2': {}}
{'key1': {'internal_key1': {'a': 'va', 'c': 'vc', 'b': 'vb'}, 'internal_key3': 'internal_v3', 'internal_key2': {'t': 1}}, '_id': '2019-01-03T00:00:00Z', 'key2': 'v2'}
"""
复数条件的组合
组合多个条件
## 组合多个条件
find_records = db_collection.find(filter={"key1.internal_key1.b":{"$exists":True},"key1.internal_key1.c":{"$exists":True}})
"""
{'_id': '2019-01-02T00:00:00Z', 'key2': None, 'key1': {'internal_key2': None, 'internal_key1': {'b': None, 'c': None, 'a': None}, 'internal_key3': None}}
{'_id': '2019-01-02T12:00:00Z', 'key2': {}, 'key1': {'internal_key2': {'t': 1}, 'internal_key1': {'b': {}, 'c': {}, 'a': {}}, 'internal_key3': 'internal_v3'}}
{'_id': '2019-01-03T00:00:00Z', 'key2': 'v2', 'key1': {'internal_key2': {'t': 1}, 'internal_key1': {'b': 'vb', 'c': 'vc', 'a': 'va'}, 'internal_key3': 'internal_v3'}}
"""
find_records = db_collection.find(filter={"key1.internal_key1.b":{"$exists":True},"key1.internal_key1.c":{"$exists":True},"_id":{"$gte":"2019-01-02T12:00 :00Z"}})# $gte ⼤于等于
"""
特种兵之火凤凰演员表{'_id': '2019-01-02T12:00:00Z', 'key2': {}, 'key1': {'internal_key3': 'internal_v3', 'internal_key1': {'b': {}, 'a': {}, 'c': {}}, 'internal_key2': {'t': 1}}}
{'_id': '2019-01-03T00:00:00Z', 'key2': 'v2', 'key1': {'internal_key3': 'internal_v3', 'internal_key1': {'b': 'vb', 'a': 'va', 'c': 'vc'}, 'internal_key2': {'t': 1}}}
"""
对同⼀个字段,多个限制条件
# 对同⼀个字段,多个限制条件。
绿循环圈外传攻略>磁盘写保护怎么取消find_records = db_collection.find(filter={"_id":{"$gte":"2019-01-01T12:00:00Z","$lt":"2019-01-03T00:00:00Z"}})# $lt ⼩于
"""
{'key1': {'internal_key1': {'a': 'va', 'c': 'vc'}, 'internal_key2': {}, 'internal_key3': 'internal_v3'}, '_id': '2019-01-01T12:00:00Z'}
{'key1': {'internal_key1': {'a': None, 'b': None, 'c': None}, 'internal_key2': None, 'internal_key3': None}, 'key2': None, '_id': '2019-01-02T00:00:00Z'} {'key1': {'internal_key1': {'a': {}, 'b': {}, 'c': {}}, 'internal_key2': {'t': 1}, 'internal_key3': 'internal_v3'}, 'key2': {}, '_id': '2019-01-02T12:00:00Z'} """
find_records = db_collection.find(filter={"_id":{"$gte":"2019-01-02T00:00:00Z","$regex":".*T00:00:00Z"}})# $regex 正则匹配
"""
{'_id': '2019-01-02T00:00:00Z', 'key1': {'internal_key3': None, 'internal_key1': {'b': None, 'c': None, 'a': None}, 'internal_key2': None}, 'key2': None} {'_id': '2019-01-03T00:00:00Z', 'key1': {'internal_key3': 'internal_v3', 'internal_key1': {'b': 'vb', 'c': 'vc', 'a': 'va'}, 'internal_key2': {'t': 1}}, 'key2': 'v2'} """
使⽤ and、or 等逻辑运算组合筛选条件
九阴真经神门十三剑$and 逻辑与,条件均满⾜才能通过筛选
find_records = db_collection.find(filter={"$and":[{"key1.internal_key1.b":{"$exists":True}},{"key1.internal_key1.c":{"$exists":True}}]})
# 与filter={"key1.internal_key1.b": {"$exists": True}, "key1.internal_key1.c": {"$exists": True}}效果⼀样
"""
{'_id': '2019-01-02T00:00:00Z', 'key2': None, 'key1': {'internal_key2': None, 'internal_key1': {'c': None, 'a': None, 'b': None}, 'internal_key3': None}} {'_id': '2019-01-02T12:00:00Z', 'key2': {}, 'key1': {'internal_key2': {'t': 1}, 'internal_key1': {'c': {}, 'a': {}, 'b': {}}, 'internal_key3': 'internal_v3'}}
{'_id': '2019-01-03T00:00:00Z', 'key2': 'v2', 'key1': {'internal_key2': {'t': 1}, 'internal_key1': {'c': 'vc', 'a': 'va', 'b': 'vb'}, 'internal_key3': 'internal_v3'}} """
$or 逻辑或,满⾜⼀条即可通过筛选
find_records = db_collection.find(filter={"$or":[{"key1.internal_key1.b":{"$exists":True}},{"key1.internal_key1.c":{"$exists":True}}]})
"""
{'_id': '2019-01-01T00:00:00Z', 'key1': {'internal_key1': {'a': 'va', 'b': 'vb'}}, 'key2': 'v2'}
{'_id': '2019-01-01T12:00:00Z', 'key1': {'internal_key1': {'a': 'va', 'c': 'vc'}, 'internal_key3': 'internal_v3', 'internal_key2': {}}}
{'_id': '2019-01-02T00:00:00Z', 'key1': {'internal_key1': {'a': None, 'b': None, 'c': None}, 'internal_key3': None, 'internal_key2': None}, 'key2': None} {'_id': '2019-01-02T12:00:00Z', 'key1': {'internal_key1': {'a': {}, 'b': {}, 'c': {}}, 'internal_key3': 'internal_v3', 'internal_key2': {'t': 1}}, 'key2': {}}
{'_id': '2019-01-03T00:00:00Z', 'key1': {'internal_key1': {'a': 'va', 'b': 'vb', 'c': 'vc'}, 'internal_key3': 'internal_v3', 'internal_key2': {'t': 1}}, 'key2': 'v2'} """
end渝是哪里的简称
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论