'NestJS Mongodb Compoud Index

I am currently writing mongodb schema in nest js.

My schema is below

{
_id: {
  filed1: string,
  filed2: string,
  field3: string,
  field4: {
    filed5: int
  }
}
filed1: ~
...
...
  1. Is there a way to create nestJs schema for the collection?
  2. Is there a way to set the index for the _id field?

I've tried the following, but it didn't work.

import { DateTimeScalar } from '@/common/scalars/dateTime.scalar';
import { Field, Float, ID, Int, ObjectType } from '@nestjs/graphql';
import { Prop, raw, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';


export type Document = MongooseClass & Document;

@ObjectType()
export class filed4 {
  @Field(() => Int)
  filed5: number;
}

@ObjectType()
export class compundId {
  @Field(() => String)
  filed1: string;

  @Field(() => String)
  filed2: string;

  @Field(() => String)
  filed3: string;

  @Field(() => field4)
  field4: field5;
}

@ObjectType()
@Schema({ collection: 'collection' })
export class MongooseClass {

  @Field(() => ID, { nullable: false })
  @Prop(
    raw({
      filed1: { type: String },
      filed2: { type: String },
      filed3: { type: string },
    }),
  )
  _id: compundId;
 
 filed ...
}

const MongooseSchema = SchemaFactory.createForClass(MongooseClass);

MongooseSchema.index({ _id: { filed1: 1, filed5: 1 } });
MongooseSchema.index({ filed2: 1, filed3: 1 });
MongooseSchema.index({ filed5: 1 });

export default ExcuteRateInfoMongooseSchema;

After checking the documentation, I couldn't find an answer to that question. please help me



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source