Home > front end >  Fetching Data based on conditions using pymongo
Fetching Data based on conditions using pymongo

Time:04-14

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

CodePudding user response:

The query will be like

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

CodePudding user response:

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"]}})
  • Related