0

Im trying to delete a record in my mongodb collection that look like this

{
    "name": "do sftp backup",
    "data": {
        "scheduleId": 95
    },
    "priority": 0,
    "type": "normal",
    "nextRunAt": null,
    "lastModifiedBy": null,
    "lockedAt": {
        "$date": "2021-08-03T23:02:44.879Z"
    },
    "lastRunAt": {
        "$date": "2021-08-03T23:02:54.874Z"
    }
}

but in need to do the query to find this record by the scheduleId in the data field

this is what im doing in the controller

db.collection('agendaJobs').deleteOne({data: scheduleId}, (error) => {
      if(!error) {
          console.log('Successfully deleted ');
          console.log(error);
          client.close();

      } else {
          console.log(error);
          client.close();
      }
  })

and getting the id by params through postman like this localhost:3000/api/schedule/95

Erraco
  • 138
  • 1
  • 12
  • 1
    Does this answer your question? [How to query nested objects?](https://stackoverflow.com/questions/16002659/how-to-query-nested-objects) – Joe Aug 03 '21 at 23:27
  • i tried both ways specified in that question, like this const findResult = await collection.find({data: {scheduleId: "95"}}) const findResult = await collection.find({'data.scheduleId':"95"}) but none of them gives me a result, the second one returns a result but nothing to do with the looking document – Erraco Aug 03 '21 at 23:43
  • also tried doing {'data.scheduleId':95} in the query input in the mongoDB compass and it returns the matching document, but in the code even if is hardcored like this const findResult = await collection.find({'data.scheduleId': 95}) doesnt work – Erraco Aug 03 '21 at 23:48
  • UPDATE: i get it working following the question submit by Joe, since find didnt give a readable result for querying i did collection.findOneAndDelete({'data.scheduleId': id}) and it worked – Erraco Aug 03 '21 at 23:58

0 Answers0