W
W
wufapexef2018-03-24 23:05:03
MongoDB
wufapexef, 2018-03-24 23:05:03

How to perform findOneAndUpdate on an array of _id?

There is an array of IDs How to execute for each element from the array and then return a response to the client about a successful operation? Should I do a cycle? Now I have something like this
const array = ['c454tv354tv5', 'vgrge45t435tv3']
findOneAndUpdate

exports.addArticle = (req, res, next) => {
  const newArticle = new Article(req.body)
  newArticle
    .save()
    .then(article => {
      Tag
        .findOneAndUpdate({ _id: article.tags }, {$push: {"articles": article._id}}, {new: true})
        .then(data => {
          return res.status(201).json({
            status: 'OK',
            data
          })
        })
    })
    .catch(err => {
      return next(err)
    })
}

When the array has 1 element, everything works like that. But when there are several of them, it does not work.

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