How to create unique fields with ottoman like mongoosejs

How to create unique fields with ottomanjs like above

import { Schema } from "mongoose";

const userSchema = new Schema({
  username: { type: String, unique: true },
  email: { type: String, unique: true },

Hey @chawki,

Couchbase does not directly support unique keys other than the primary key. There are however two options available to you, depending on your use case. One would be to override the primary key used by Ottoman (via the id option) to be whichever property you wanted to be unique. The other option is to specify a refdoc index against the fields you want to keep unique. refdoc indexes are naturally unique as they use reference documents where the primary key is the field you’ve specified. It can however be complicated to resolve some cases when you specify multiple refdoc indexes and only a subset of them collide. Again, let me know if you have any more questions!

Cheers, Brett

1 Like