如何在使用mongoose时允许子文档中的唯一字段?

huangapple go评论75阅读模式
英文:

How to allow unique fields in subdocuments when using mongoose?

问题

以下是翻译好的代码部分:

employee.js

const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");
const Role = require("./role");

const employeeSchema = mongoose.Schema({
  code: { type: String, required: true, unique: true, index: true },
  names: { type: String, required: true },
  last_names: { type: String, required: true },
  role: Role.schema,
  dui: { type: String, required: true, unique: true, index: true },
  nit: { type: String, required: false, unique: true, index: true },
  sex: { type: String, required: false },
  civil_status: { type: String, required: false },
  birthday: { type: Date, required: false },
  telephone: { type: String, required: true },
  city: { type: String, required: true },
  address: { type: String, required: true },
  active: { type: Boolean, required: true },
});

employeeSchema.plugin(uniqueValidator);

module.exports = mongoose.model("Employee", employeeSchema);

role.js

const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");

const roleSchema = mongoose.Schema({
  code: { type: String, required: true, unique: true, index: true },
  description: { type: String, required: true }
});

roleSchema.plugin(uniqueValidator);

module.exports = mongoose.model("Role", roleSchema);

如果您有关于代码或问题的进一步疑问,请随时提出。

英文:

I'm using mongoose to to define 2 schemas.

employee.js

const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");
const Role = require("./role");

const employeeSchema = mongoose.Schema({
  code: { type: String, required: true, unique: true, index: true },
  names: { type: String, required: true },
  last_names: { type: String, required: true },
  role: Role.schema,
  dui: { type: String, required: true, unique: true, index: true },
  nit: { type: String, required: false, unique: true, index: true },
  sex: { type: String, required: false },
  civil_status: { type: String, required: false },
  birthday: { type: Date, required: false },
  telephone: { type: String, required: true },
  city: { type: String, required: true },
  address: { type: String, required: true },
  active: { type: Boolean, required: true },
});


employeeSchema.plugin(uniqueValidator);

module.exports = mongoose.model("Employee", employeeSchema);

and role.js

const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");

const roleSchema = mongoose.Schema({
  code: { type: String, required: true, unique: true, index: true },
  description: { type: String, required: true }
});

roleSchema.plugin(uniqueValidator);

module.exports = mongoose.model("Role", roleSchema);

The problem I have is that, whenever I insert a document with a role code that's repeated I get an error because of the unique validation. I have tried deleting

roleSchema.plugin(uniqueValidator);

from role.js and I have also tried using .set to alter the field in role.

const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");
const Role = require("./role");

const subRole = Role.schema.clone().set('code', {unique: false}).set('code', {index : false});

const employeeSchema = mongoose.Schema({
  code: { type: String, required: true, unique: true, index: true },
  names: { type: String, required: true },
  last_names: { type: String, required: true },
  role: subRole,
  dui: { type: String, required: true, unique: true, index: true },
  nit: { type: String, required: false, unique: true, index: true },
  sex: { type: String, required: false },
  civil_status: { type: String, required: false },
  birthday: { type: Date, required: false },
  telephone: { type: String, required: true },
  city: { type: String, required: true },
  address: { type: String, required: true },
  active: { type: Boolean, required: true },
});


employeeSchema.plugin(uniqueValidator);

module.exports = mongoose.model("Employee", employeeSchema);

I always get an error saying that role code needs to be unique. Am I using .set incorrectly? or what am I missing? Thank you very much in advance.

答案1

得分: 0

已解决方法如下:

将以下代码段进行更改:

const subRole = Role.schema.clone().set('code', { unique: false }).set('code', `{index : false});`

更改为:

const subRole = Role.schema.clone().set('excludeIndexes', true);

删除了员工集合,以便重新创建带有正确索引的集合,这样问题就解决了。

在 mongoose 的 GitHub 问题中提到了这一点:
https://github.com/Automattic/mongoose/issues/11547

据说他们对此做了一些实现,但我没有明白。不过,上述方法对我有效。

英文:

Solved it changing:

const subRole = Role.schema.clone().set('code', {unique: false}).set('code', `{index : false});`

to

const subRole = Role.schema.clone().set('excludeIndexes', true);

Deleted the employees's collection so it would recreate it with the right indexes and that did the trick.

It was mentioned in one of mongoose's issues in github:
https://github.com/Automattic/mongoose/issues/11547

Supposedly they implemented something about it but I didn't get it. The above worked for me though.

huangapple
  • 本文由 发表于 2023年1月7日 05:49:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75036387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定