71

如果我有这样的架构:

var person = new Schema({
  firstName:  String,
  lastName: String,
});

我想确保只有一个文件具有相同的名字和姓氏。

我怎样才能做到这一点?

4

6 回答 6

124

index您可以使用对架构的调用来定义唯一的复合索引:

person.index({ firstName: 1, lastName: 1}, { unique: true });
于 2013-04-17T18:21:04.017 回答
32

我最近才通过对 Mongoose 的实验发现的有趣的小东西。例如,我有以下架构:

const ShapesSchema = new mongoose.Schema({
  name: { type: String, required: true },
  user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
})

ShapesSchema.index({ name: 1, user: 1 }, { unique: true })

mongoose.model('Shapes', ShapesSchema)

这个想法是创建一个独一无二的复合name索引user。这样,只要每个形状都有一个不同的名称,用户就可以创建任意数量的形状。反过来也应该是正确的——形状可以有相同的名称,只要它们有不同的用户。对我来说不是这样。

我注意到除了 on 的索引之外_id,还创建了其他三个索引条目。, ,各有一个nameuser并且name_user都设置为唯一。我对架构进行了修改,并将其包含unique: false在我用于复合索引的每个字段中,突然间一切都按预期工作了。我最终得到的是:

const ShapesSchema = new mongoose.Schema({
  name: { type: String, required: true, unique: false },
  user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', unique: false }
})

ShapesSchema.index({ name: 1, user: 1 }, { unique: true })

mongoose.model('Shapes', ShapesSchema)

查看结果创建的索引,我仍然看到三个索引 - nameusername_user。但不同之处在于,前两个不是唯一的,而最后一个是化合物。现在我的每个用户使用多个不同形状的用例,或者具有不同用户的同名形状就像一个冠军。

于 2018-03-22T04:22:35.487 回答
3

像这样定义你的架构


var person = new Schema({
firstName:  String,
lastName: String,
index: true,
unique: true, 

});

或者


person.index({ firstName: 1, lastName: 1}, { unique: true });

于 2019-01-27T18:11:13.387 回答
1
const personSchema = new Schema({ firstName:  String, lastName: String });
const person = mongoose.model('recipients', personSchema);
person.createIndexes();

您可能需要删除集合中的所有重复项,或者以更快更简单的方式进行操作:

编辑您的代码,删除集合,然后重新启动 Mongo。

于 2019-05-08T07:58:21.093 回答
0

我还没有尝试过,但是使用唯一索引应该可以解决问题。

db.person.ensureIndex( { "firstname": 1, "lastname": 1 }, { unique: true } )
于 2013-04-17T13:51:23.463 回答
-2

您可以像这样定义您的架构。

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const bcrypt = require("bcryptjs");

    const userSchema = new Schema({
      firstName: {
        trim: true,
        type: String,
        required: [true, "firstName is required!"],
        validate(value) {
          if (value.length < 2) {
            throw new Error("firstName is invalid!");
          }
        }
      },
      lastName: {
        trim: true,
        type: String,
        required: [true, "lastName is required!"],
        validate(value) {
          if (value.length < 2) {
            throw new Error("lastName is invalid!");
          }
        }
      },
      username: {
        unique: [true, "Username already available"],
        type: String,
        required: [true, "Username is required"],
        validate(value) {
          if (value.length < 10) {
            throw new Error("Username is invalid!");
          }
        }
      },
      mobile: {
        unique: [true, "Mobile Number alraedy available"],
        type: String,
        required: [true, "Mobile Number is required"],
        validate(value) {
          if (value.length !== 10) {
            throw new Error("Mobile Number is invalid!");
          }
        }
      },
      password: {
        type: String,
        required: [true, "Password is required"],
        validate(value) {
          if (value.length < 8) {
            throw new Error("Password is invalid!");
          }
        }
      },
      gender: {
        type: String
      },
      dob: {
        type: Date,
        default: Date.now()
      },
      address: {
        street: {
          type: String
        },
        city: {
          trim: true,
          type: String
        },
        pin: {
          trim: true,
          type: Number,
          validate(value) {
            if (!(value >= 100000 && value <= 999999)) {
              throw new Error("Pin is invalid!");
            }
          }
        }
      }
      date: { type: Date, default: Date.now }
    });
于 2019-12-15T10:31:36.480 回答