0

我目前正在用 Nest js 编写 mongodb 模式。

我的架构如下

{
_id: {
  filed1: string,
  filed2: string,
  field3: string,
  field4: {
    filed5: int
  }
}
filed1: ~
...
...
  1. 有没有办法为集合创建 nestJs 模式?
  2. 有没有办法为 _id 字段设置索引?

我尝试了以下方法,但没有奏效。

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;

检查文档后,我找不到该问题的答案。请帮我

4

0 回答 0