根据pymongo根据条件获取数据

发布于 2025-01-21 17:39:01 字数 1158 浏览 2 评论 0原文

我正在尝试根据基于过滤器的查询获取数据。现在,我正在使用

 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_predml_pred将具有值有效 无效null

具有字段值的获取数据hum_pred为nullml_pred有效或无效

示例数据

[
 {"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"}]

I am trying to fetch data based on filter based queries. Right now I am using

 filter_stuff = {'url': 1, 'organization_name': 1, 'hum_pred':1, 'ml_pred':1, '_id': 0}
 myData = list(crawlcol.find({'hum_pred': 'null'}, filter_stuff))

This will fetch data where hum_pred have values null.

hum_pred and ml_pred will have values valid invalid or null

How is it possible to fetch data which has field values hum_pred is null and ml_pred is valid or invalid

sample data

[
 {"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"}
 ]

Expected Output

[{"url":"www.example2.com","organization_name":"gvg","ml_pred":"Invalid", "hum_pred":"null"}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℡寂寞咖啡 2025-01-28 17:39:01

查询就像

myData = list(crawlcol.find({"$and": [{"ml_pred": {"$ne": "null"}},{"hum_pred": {"$eq": "null"}}]}))

The query will be like

myData = list(crawlcol.find({"$and": [{"ml_pred": {"$ne": "null"}},{"hum_pred": {"$eq": "null"}}]}))
梦在夏天 2025-01-28 17:39:01

查询

  • 您的字符串例如“ null”,因此使用字符串用于null
  • 以检查是否与多个值之一,我们可以使用$或and&and&and&and $ eq或 in 喜欢bellow( in in 的 $ in CODACT)

*如果您只有3个可能的值ML_PRED @imhans4305
答案甚至更短,所以我认为另一个答案是在通常的情况下,这就是您可以做到的。

playmongo

find({"hum_pred": "null","ml_pred": {"$in": ["Valid", "Invalid"]}})

Query

  • you have strings for example "null", so strings are used for null
  • to check if equal with one of multiple values we can use $or and $eq or $in like bellow ($in is more compact for this use)

*if you have only 3 possible values for ml_pred the @imhans4305
answer is even shorter, so go for the other i think, this is how you could do it in general case.

Playmongo

find({"hum_pred": "null","ml_pred": {"$in": ["Valid", "Invalid"]}})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文