【问题标题】:How to set the unique property for Schema by using mongoose?如何使用 mongoose 为 Schema 设置唯一属性?
【发布时间】:2019-02-16 01:37:35
【问题描述】:

我在 NodeJS 中使用 Mongoose 来操作 MongoDB 数据库。我遇到的一个问题是,当我定义一个模式时,我想将该模式中的一个字段设置为唯一的。我按照the instructions of the official document设置了字段,但是没有用。有人知道在定义Schema的时候,如何定义一个字段值是唯一的呢?我使用的Mongoose版本是V5.2.13。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您只需添加unique: true 例如:

    const mySchema = new Schema({
        test: {
            type: String,
            unique: true
        }
    });
    

    【讨论】:

    • 是的,我这样做了。但它没有用。请注意,我使用的猫鼬版本是 V5.2.13。我在 Github 上看到了你的答案。
    猜你喜欢
    • 2019-10-01
    • 2018-10-09
    • 1970-01-01
    • 2020-07-04
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    相关资源
    最近更新 更多