【问题标题】:Mongoose save() does not update databaseMongoose save() 不更新数据库
【发布时间】:2021-08-28 00:55:03
【问题描述】:

我正在使用 MongoDB 版本 4.4.8 和 mongoose 版本 5.13.5 处理要下载的文件包,但遇到了问题。每当从包中删除文件时,它都不会更新。这是我正在尝试运行的代码:

app.post('/remove-package-file', (req, res, next) => {
      let { package, file } = req.body
      if (!package || !file) return res.status(500).json({ error: true, message: 'Please specify file and package to remove' })
      Package.findOne({ _id: package }).then(pack => {
        if (!pack) return res.status(500).json({ error: true, message: 'Unable to locate package' })
        if (!pack.files.includes(file)) return res.status(500).json({ error: true, message: 'File is not in package' })
        File.findOne({ _id: file }).then(file => {
          if (!file) return res.status(500).json({ error: true, message: 'Unable to locate file' })
          pack.files.pull(file._id)
          pack.size = pack.size-file.stats.size
          pack.markModified('size')
          pack.markModified('files')
          pack.save().then(result => {
            console.log(result)
            Package.findOne({_id: result._id}).then(result2 => {
              console.log(result2)
              res.status(200).json({ error: false, package: result2 })
            }).catch(e => {
              saveError(e)
              return res.status(500).json({ error: true, message: 'DB error' })
            })
          }).catch(e => {
            saveError(e)
            return res.status(500).json({ error: true, message: 'DB error' })
          })
        }).catch(e => {
          saveError(e)
          return res.status(500).json({ error: true, message: 'DB error' })
        })
      }).catch(e => {
        saveError(e)
        return res.status(500).json({ error: true, message: 'DB error' })
      })
    })

输出:

{
  titles: [ '6127d98a5b5df7462cb166d5' ],
  files: [ '6127da305b5df7462cb166db' ],
  accessLog: [],
  successfulDownloads: [],
  timesDownloaded: 0,
  recipients: [ '6126da9ac081145ab46039e6' ],
  _id: 6128363fc03f9d23f86258ac,
  size: -281171,
  expires: 2021-08-28T00:47:59.654Z,
  name: 't2',
  createdBy: '6126da9ac081145ab46039e6',
  link: 'http://localhost:4232/download-package?_id=6128363fc03f9d23f86258ac',
  date: 2021-08-27T00:47:59.658Z,
  __v: 7
}
{
  titles: [ '6127d98a5b5df7462cb166d5' ],
  files: [ '6127da9c5b5df7462cb166f9', '6127da305b5df7462cb166db' ],
  accessLog: [],
  successfulDownloads: [],
  timesDownloaded: 0,
  recipients: [ '6126da9ac081145ab46039e6' ],
  _id: 6128363fc03f9d23f86258ac,
  size: -281171,
  expires: 2021-08-28T00:47:59.654Z,
  name: 't2',
  createdBy: '6126da9ac081145ab46039e6',
  link: 'http://localhost:4232/download-package?_id=6128363fc03f9d23f86258ac',
  date: 2021-08-27T00:47:59.658Z,
  __v: 7
}

这表明文件已正确更新,但当随后查询时,文件数组中的值仍然相同,但大小已更改,并且具有新的版本号。如果您知道为什么会发生这种情况,请告诉我。

【问题讨论】:

  • 您使用的是哪个 mongodb 版本?
  • 我使用的是4.4.8版

标签: javascript node.js mongodb mongoose


【解决方案1】:

您使用的是哪个 MongoDB 版本?

Mongo v5.0 和更高版本不推荐使用 .save() 函数,替代方法是 .insert() / .insertMany()

根据您执行更新操作的代码,您可以使用 .updateOne() 或 .updateMany()

https://docs.mongodb.com/v5.0/release-notes/5.0-compatibility/ 在这里你会发现不推荐使用的函数

【讨论】:

  • 4.4.8 是我使用的版本
【解决方案2】:

我之前没有见过任何pull 方法,如pack.files.pull(file._id)

如果该功能实际上不存在/不工作,可以使用这样的过滤器:

pack.files = pack.files.filter((str) => str !== file._id)

【讨论】:

  • 感谢您的回复。这是文件从猫鼬那里得到的一种方法。从控制台日志看来,从阵列中删除 _id 似乎是可行的。但是,当我稍后再次查询文档时,它已经丢失了对数组的更改。
【解决方案3】:

我找到的解决方案是我需要在我的猫鼬模型中将字段的类型定义为 [String] 而不是 Array,如下所示:

const mongo = require('mongoose');
const Schema = mongo.Schema({
    _id: mongo.Schema.Types.ObjectId,
    files: [String],
    link: String
})
module.exports = mongo.model('Directory', Schema, 'Directory');

这使得 objectIds 始终被视为数组中的字符串,而不是在使用 mongoose 的 $update.pull 方法时不匹配字符串的 objectIds。

这很令人困惑,因为我在保存时使用 new mongoose.Types.ObjectId().toHexString() 将 _ids 保存为字符串,但我想这些被转换回对象。

【讨论】:

    猜你喜欢
    • 2016-02-05
    • 2014-08-28
    • 2017-01-16
    • 2014-10-27
    • 2014-02-17
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2013-02-26
    相关资源
    最近更新 更多