Replace substring in mongoDB document's *array* with a single DB call

Viewed 17

My mongoDB documents have the following structure:

{
  _id: 'objectID',
  isFlexible: true, // || false
  date: 'date type string',
  availability: ['thursday.morning', 'thursday.afternoon', 'thursday.evening']
}

I want to be able to select some of the documents by isFlexible: true and change the date to today's date (this is easy) and also change the availability array to become:

availability: ['friday.morning', 'friday.afternoon', 'friday.evening']

I know I can do this this if I first find all documents, modify them inside the nodeJS/expressJS API and save them afterwards but I'm looking for a solution where this can be done in a single call to mongoDB.

Any help is greatly appreciated.

1 Answers

You can build the new availability: ['friday.morning', 'friday.afternoon', 'friday.evening'] inside the API and send it to be saved inside the DB as is instead of trying to change it inside the query.

Related