How do I execute the following functionality in n the given mongoDB database:Write a query that deletes all patients that have cold as a disease

[
  {
    _id: ObjectId("641e3c070fb20438f24d1659"),
    firstName: 'Max',
    lastName: 'Chavan',
    age: 29,
    history: [
      { disease: 'cold', treatment: 'steam' },
      { disease: 'malaria', treatment: 'injection' }
    ]
  },
  {
    _id: ObjectId("641e3c3d0fb20438f24d165a"),
    firstName: 'Aditya',
    lastName: 'Methe',
    age: 25,
    history: [
      { disease: 'cancer', treatment: 'none' },
      { disease: 'chickengunia', treatment: 'none' }
    ]
  },
  {
    _id: ObjectId("641e3c5d0fb20438f24d165b"),
    firstName: 'Lokesh',
    lastName: 'Vakhare',
    age: 25,
    history: [
      { disease: 'fever', treatment: 'medicine' },
      { disease: 'chickenpox', treatment: 'injection' }
    ]
  }
]
[
  {
    _id: ObjectId("641e3c070fb20438f24d1659"),
    firstName: 'Max',
    lastName: 'Chavan',
    age: 29,
    history: [
      { disease: 'cold', treatment: 'steam' },
      { disease: 'malaria', treatment: 'injection' }
    ]
  },
  {
    _id: ObjectId("641e3c3d0fb20438f24d165a"),
    firstName: 'Aditya',
    lastName: 'Methe',
    age: 25,
    history: [
      { disease: 'cancer', treatment: 'none' },
      { disease: 'chickengunia', treatment: 'none' }
    ]
  },
  {
    _id: ObjectId("641e3c5d0fb20438f24d165b"),
    firstName: 'Lokesh',
    lastName: 'Vakhare',
    age: 25,
    history: [
      { disease: 'fever', treatment: 'medicine' },
      { disease: 'chickenpox', treatment: 'injection' }
    ]
  }
]

The above database is a mongoDb database.

I am trying to write a query that deletes all patients that have cold as a disease.

Here is my query,

db.patient.remove(history.{disease:"cold",treatment:'steam'})

However, the query gives the following error:

Unexpected token (1:26)

> 1 | db.patient.remove(history.{disease:"cold",treatment:'steam'})
    |                           ^
  2 |

How do I resolve it?

Look at dot notation.

Note that having

in your query will not delete documents that disease:cold but have a different treatment yet your requirement (from the post title) is delete all patients that have cold as desease. There is not mention about the treatment.

1 Like