如何用MongoDB中的findAndModify方法指示更新?

时间:2017-12-13 17:48:47

标签: mongodb

我正在使用MongoDB和Express.js处理应用。

我正在创建一个帖子处理程序,用于为玩具更新玩具(通过其id找到),该玩具的新名称被推送到包含其他提议名称的ID的nameIds数组中:

router.post('/names', (req, res) => {
  const toyId = req.body.toyId;
  const name = req.body.newName;

  mdb.collection('names').insertOne({ name }).then(result =>
   mdb.collection('toys').findAndModify({
     query: { id: toyId },
     update: { $push: { nameIds: result.insertedId } },
     new: true
   }).then(doc =>
     res.send({
       updatedToy: doc.value,
       newName: { id: result.insertedId, name }
     })
   )
  )
});

但是,当我测试时,我收到此错误:

name: 'MongoError',
message: 'Either an update or remove=true must be specified',
ok: 0,
errmsg: 'Either an update or remove=true must be specified',
code: 9,
codeName: 'FailedToParse'

我对MongoDB并不陌生,但这个简单的电话让我感到困惑。

感谢您提供的任何帮助!

0 个答案:

没有答案