How to delete an entire object based on elements in an array which is nested inside another array of objects in mongodb?

Viewed 45

I am pretty new to MongoDB. This the structure of my database hospital and collection patientData.

when I do

db.patientData.find().pretty()

I get

{
    "_id" : ObjectId("6086fbc54f25d8cf518e99c3"),
    "first name" : "olivia",
    "last name" : "jackson",
    "age" : 24,
    "history" : [
        {
            "disease" : "conjectivitus",
            "treatment" : "eyedrops"
        },
        {
            "symptoms" : [
                "eye scratching",
                "sore throat",
                "headache",
                "jointpain"
            ]
        }
    ]
}

{
    "_id" : ObjectId("6086fbc54f25d8cf518e99c4"),
    "first name" : "Doriya",
    "last name" : "Xavier",
    "age" : 27,
    "history" : [
        {
            "disease" : "para thyroid",
            "treatment" : "thyroxin"
        },
        {
            "symptoms" : [
                "bulginess",
                "sore throat",
                "headache",
                "jointpain"
            ]
        }
    ]
}
{
    "_id" : ObjectId("6086fbc54f25d8cf518e99c5"),
    "first name" : "Meenu",
    "last name" : "Danish",
    "age" : 32,
    "history" : [
        {
            "disease" : "chicken pox",
            "treatment" : "citricin"
        },
        {
            "symptoms" : [
                "itchiness",
                "pimple",
                "fever",
                "nausea"
            ]
        }

I want to delete the objects based on the symptoms of "headache". Which is a pure array.

Basicaly next time when I do,

db.patientData.find().pretty()

I only want to get this document in my collection.

{
    "_id" : ObjectId("6086fbc54f25d8cf518e99c5"),
    "first name" : "Meenu",
    "last name" : "Danish",
    "age" : 32,
    "history" : [
        {
            "disease" : "chicken pox",
            "treatment" : "citricin"
        },
        {
            "symptoms" : [
                "itchiness",
                "pimple",
                "fever",
                "nausea"
            ]
        }
    ]
}

I tried using $pull but it is not working.

Please help me with the code.

0 Answers
Related