I am trying to query a database collection that contains process documents for those documents that have certain fields. For simplicity, imagine the following general outline of a document:
{
"timestamp": ISODate("..."),
"result1": "pass",
"result2": "fail"
}
Now, when the process starts, a new document is inserted only with a time stamp. When this process reaches a certain stage, the field result1and result2added to over time. However, some processes do not reach stages 1or 2, therefore, do not have result fields.
I would like to query the database to retrieve only those documents that have BOTH result1and result2.
I know about $ exists , but as far as I can tell, this only works for one field at a time, i.e. db.coll.find({"result1": {$exists: true}}). An operator $existscannot be used as a top-level operator. For instance. this does not work :
db.coll.find({"$exists": {"result1": true, "result2": true}})
To check both results, I will need:
db.coll.find({"result1": {"$exists": true}, "result2": {"$exists": true}})
Now this is already tiring for several variables.
Is there a better way to do this? (Also, I am doing this in Python, so if there is a solution only for the pymongo driver that will make me happy.)