使用 pymongo 根据条件获取数据
Fetching Data based on conditions using pymongo
我正在尝试根据基于过滤器的查询获取数据。现在我正在使用
filter_stuff = {'url': 1, 'organization_name': 1, 'hum_pred':1, 'ml_pred':1, '_id': 0}
myData = list(crawlcol.find({'hum_pred': 'null'}, filter_stuff))
这将获取 hum_pred
具有值 null
的数据。
hum_pred
和 ml_pred
将具有值 valid
invalid
或 null
如何获取具有字段值 hum_pred is null
和 ml_pred is valid or invalid
的数据
示例数据
[
{"url":"www.example1.com","organization_name":"abc","ml_pred":"Valid", "hum_pred":"invalid"},
{"url":"www.example2.com","organization_name":"gvg","ml_pred":"Invalid", "hum_pred":"null"},
{"url":"www.example3.com","organization_name":"hsg","ml_pred":"null", "hum_pred":"null"},
{"url":"www.example4.com","organization_name":"hga","ml_pred":"Valid", "hum_pred":"valid"},
{"url":"www.example5.com","organization_name":"tre","ml_pred":"Invalid", "hum_pred":"valid"}
]
预期输出
[{"url":"www.example2.com","organization_name":"gvg","ml_pred":"Invalid", "hum_pred":"null"}]
查询会像
myData = list(crawlcol.find({"$and": [{"ml_pred": {"$ne": "null"}},{"hum_pred": {"$eq": "null"}}]}))
查询
- 您有字符串,例如
"null"
,因此字符串用于 null
- 要检查是否等于多个值之一,我们可以使用
$or and $eq
或 $in
,如下所示($in
对于此用途更紧凑)
*如果你只有 3 个可能的值 ml_pred
@imhans4305
答案更短,所以我想换另一个,这就是一般情况下你可以做的。
find({"hum_pred": "null","ml_pred": {"$in": ["Valid", "Invalid"]}})
我正在尝试根据基于过滤器的查询获取数据。现在我正在使用
filter_stuff = {'url': 1, 'organization_name': 1, 'hum_pred':1, 'ml_pred':1, '_id': 0}
myData = list(crawlcol.find({'hum_pred': 'null'}, filter_stuff))
这将获取 hum_pred
具有值 null
的数据。
hum_pred
和 ml_pred
将具有值 valid
invalid
或 null
如何获取具有字段值 hum_pred is null
和 ml_pred is valid or invalid
示例数据
[
{"url":"www.example1.com","organization_name":"abc","ml_pred":"Valid", "hum_pred":"invalid"},
{"url":"www.example2.com","organization_name":"gvg","ml_pred":"Invalid", "hum_pred":"null"},
{"url":"www.example3.com","organization_name":"hsg","ml_pred":"null", "hum_pred":"null"},
{"url":"www.example4.com","organization_name":"hga","ml_pred":"Valid", "hum_pred":"valid"},
{"url":"www.example5.com","organization_name":"tre","ml_pred":"Invalid", "hum_pred":"valid"}
]
预期输出
[{"url":"www.example2.com","organization_name":"gvg","ml_pred":"Invalid", "hum_pred":"null"}]
查询会像
myData = list(crawlcol.find({"$and": [{"ml_pred": {"$ne": "null"}},{"hum_pred": {"$eq": "null"}}]}))
查询
- 您有字符串,例如
"null"
,因此字符串用于null
- 要检查是否等于多个值之一,我们可以使用
$or and $eq
或$in
,如下所示($in
对于此用途更紧凑)
*如果你只有 3 个可能的值 ml_pred
@imhans4305
答案更短,所以我想换另一个,这就是一般情况下你可以做的。
find({"hum_pred": "null","ml_pred": {"$in": ["Valid", "Invalid"]}})