小编Rüd*_*uße的帖子

数组中的 MongoDB 更新失败:更新路径“companies.$.updatedAt”会在“companies.$”处产生冲突

我们(从 MongoDB 3.4)升级到:

MongoDB:4.2.8

猫鼬:5.9.10

现在我们收到了这些错误。对于最小的例子,模型是:

[公司.js]

'use strict';

const Schema = require('mongoose').Schema;

module.exports = new Schema({
  name: {type: String, required: true},
}, {timestamps: true});

Run Code Online (Sandbox Code Playgroud)

[target_group.js]

'use strict';

const Schema = require('mongoose').Schema;

module.exports = new Schema({
  title: {
    type: String,
    required: true,
    index: true,
  },
  minAge: Number,
  maxAge: Number,
  companies: [Company],
}, {timestamps: true});

Run Code Online (Sandbox Code Playgroud)

当我尝试更新目标组内的公司时

  _updateTargetGroup(companyId, company) {
    return this.targetGroup.update(
      {'companies._id': companyId},
      {$set: {'companies.$': company}},
      {multi: true});
  }
Run Code Online (Sandbox Code Playgroud)

我收到

MongoError:更新路径“companies.$.updatedAt”会在“companies.$”处产生冲突

即使我前置

    delete company.updatedAt;
    delete company.createdAt;
Run Code Online (Sandbox Code Playgroud)

我收到这个错误。

如果我尝试类似的数据库工具(Robo3T),一切正常:

db.getCollection('targetgroups').update(
  {'companies.name': …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb

7
推荐指数
1
解决办法
2373
查看次数

标签 统计

mongodb ×1

mongoose ×1