0

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"}]
5
  • You can specify multiple conditions on multiple fields using $or and $and logical query operators. Commented Apr 14, 2022 at 10:15
  • @prasad_ Thankyou for the response. Can you give me an example Commented Apr 14, 2022 at 10:19
  • Please feel free to refer the MongoDB Manual, for the usage and examples for the query operators. Commented Apr 14, 2022 at 10:21
  • I appied in this way list(crawlcol.find({$and: [{$or: [{"ml_pred": "Valid"},{"ml_pred": "Invalid"}]}],{"hum_pred": "null"}}, filter_stuff)) but I am getting an error SyntaxError: invalid syntax Commented Apr 14, 2022 at 10:56
  • Also, see PyMongo documentation for writing queries with Python specific syntax. With Python code one of the requirements is that the operators and field names must be within quotes; for example "$or", 'hum_pred', .... Commented Apr 14, 2022 at 11:02

2 Answers 2

1

The query will be like

myData = list(crawlcol.find({"$and": [{"ml_pred": {"$ne": "null"}},{"hum_pred": {"$eq": "null"}}]}))
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Thaank for the reply. Please upvote the question too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.