Is there a way to ignore a parameter when querying documents using PyMongo in one line? For example:
db.find_one( { "first_name": firstname, "last_name": lastname, "age": age if age else <ignore this field from querying all together> } ):
Is there a way to ignore a parameter when querying documents using PyMongo in one line? For example:
db.find_one( { "first_name": firstname, "last_name": lastname, "age": age if age else <ignore this field from querying all together> } ):
2
Answers
Work with
$and
and$or
operators.$and
– Match 1.1. and 1.2. conditions.1.1. Match
first_name
andlast_name
.1.2.
$or
– Matchage
variable is null orage
field match withage
variable.Sample Mongo Playground
Here are couple of ways you can try:
-OR-
-OR- (same as above)