I
I
Ihor S2021-05-04 10:41:19
Mongoose
Ihor S, 2021-05-04 10:41:19

Is it possible to combine into one or two similar mongoDB queries?

There is a request that sends the name of the location. I need to change the location data, but for this I first have to find the location, then change it. Is there a way to do this all in one request?

const oldLoc = await LocationModel.findOne({ name: winnerHero.ownerPlace });

const cityArr = [...oldLoc.cities]
const selectedCity = cityArr[winnerHero.cityId]
const newCity = {
  ...selectedCity,
  owner: '',
  ownerId: '',
}
cityArr.splice(winnerHero.cityId, 1, newCity)

await LocationModel.findOneAndUpdate({ name: winnerHero.ownerPlace }, { cities: cityArr });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question