【问题标题】:Get fields set to unique in schema from Mongoose从 Mongoose 获取模式中设置为唯一的字段
【发布时间】:2016-03-09 23:01:08
【问题描述】:

我想检查schema 中的哪些字段设置为unique,类似于通过MyCollection.collection.getIndexes() 获取schemaindexes。可以在schema 对象的某处找到此信息吗?

【问题讨论】:

    标签: node.js mongodb mongoose schema unique


    【解决方案1】:

    试试这个:

    var schema = new mongoose.Schema({
      a: {
        type: String,
        unique: true
      },
      b: {
        type: String
      }
    });
    
    schema.tree.a.unique; // true
    schema.tree.b.unique; // undefined
    
    schema.path('a').options.unique; // true
    schema.path('b').options.unique; // undefined
    

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 2012-05-08
      • 2016-09-11
      • 2021-11-15
      • 2019-11-06
      • 2012-12-09
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多