0

I'm using Nest.js and trying to create a strict schema for the ts below:

      interface SchemaForMongo {
      [key: string]: ObjectID[]
    }
    
    const invalidDocumentProperty_1: SchemaForMongo = {validKey : mongoose.Types.ObjectId('4edd40c86762e0fb12000003')}  // invalid value
    const invalidDocumentProperty_2: SchemaForMongo = {validKey : ['4edd40c86762e0fb12000003']} // invalid value
    const invalidDocumentProperty_3: SchemaForMongo = {validKey : ['invalid value']} // invalid value
    const invalidDocumentProperty_4: SchemaForMongo = {33 : [mongoose.Types.ObjectId('4edd40c86762e0fb12000003')]} // invalid key
    
    const validDocumentProperty: SchemaForMongo = {validKey: [mongoose.Types.ObjectId('4edd40c86762e0fb12000003')]} // valid key and value
ENcy
  • 324
  • 4
  • 7

1 Answers1

1

I may be wrong, but if my memories are good, mongoDB does not support dictionaries. (check here)

So i think the only option you have to do what you want is something like this:

interface MongoKeyValuePair {
  key: string
  value: mongoose.types.ObjectId[]
}

export class ModelWithDictionary extends Document {
  @Prop({ type: MongoKeyValuePair, default: [] })
  dictionary: MongoKeyValuePair[]
}

Kharente Deuh
  • 206
  • 2
  • 6