1

I am working on an API using FastAPI that interacts with a Mongo DB via Motor. Our database contains documents on various illnesses such that look like this:

{
    "_id": {
        "$oid": "6008b21ba1285a480088ff48"
    },
    "names": ["heart_attack", "myocardial_infarction"],
    "umls": ["C0027051"],
    "risk_factors": ["smoking", "age < 45", "male", "obesity", "etc"],
    "risk_factors_umls": ["C0037369", "CXXXXXXX"],
    "symptoms_umls": ["C0008031", "etc."], # ids for symptoms 
    "is_emergency": true
}

I am looking for the correct MongoDB query syntax (ideally with a small python code example) that given a list of risk factors or symptom ids returns all illness documents containing one or more of these ids or strings.

Most Illnesses have a different amount of symptoms, so the order of the ids varies from document to document.

Since I'm very new to mongoDB I am not sure what the correct query would look like. I tried multiple variations of illness_collection.find() but so far I haven't been sucessful.

Thank you in advance for your help!

Moritz
  • 11
  • 1

1 Answers1

0

you can do that with a combination of $or and $in operators like so:

let symptomList = ["smoking", "C0008031"];

db.collection.find(
{
    $or: [
        { risk_factors: { $in: symptomList } },
        { symptoms_umls: { $in: symptomList } }
    ]

})
Dĵ ΝιΓΞΗΛψΚ
  • 5,068
  • 3
  • 13
  • 26