代码之家  ›  专栏  ›  技术社区  ›  Kihats

MongoDb在不是所有文档中的字段上创建唯一索引[duplicate]

  •  0
  • Kihats  · 技术社区  · 6 年前

    我知道没有新字段的文档 null

    2 回复  |  直到 6 年前
        1
  •  1
  •   Rishabh Agarwal    6 年前

    你可以用 Sparse Index 只包含具有索引字段的文档的条目。例子:

    db.collection.createIndex( { "newIndex": 1 }, { sparse: true } )
    
        2
  •  2
  •   Alex Blex    6 年前

    partial index

    db.collection.createIndex(
       { newField: 1 },
       { unique: true, partialFilterExpression: { newField: { $exists: true } } }
    )