Estoy enfrentando un error con typegoose. Tengo un modelo llamado SP y tengo que crear un índice 2dsphere en su propiedad llamada geoLocation . Probé typegoose decorator @index pero no funciona, incluso no arroja ningún error, no sé qué está pasando y cómo lo maneja typegoose. Hay alguien que me dé la solución de eso 👆.
código:
import { Prop, index, modelOptions, Severity } from '@typegoose/typegoose'; import { BaseModel } from '../base.model'; import { Factory } from 'nestjs-seeder'; export enum SPStatus { INAVTIVE = "INAVTIVE", ACTIVE = "ACTIVE", } @modelOptions({ options: { allowMixed: Severity.ALLOW } }) @index({ geoLocation: '2dsphere' }, {}) export class SP extends BaseModel{ @Factory(faker => faker.company.companyName()) @Prop({ required: true, index: true, text: true }) businessName : string @Factory(faker => { let data = { type : "Point", coordinates:[Number(faker.address.longitude()), Number(faker.address.latitude())] } console.log(data); return data; }) @Prop({ required: false }) geoLocation: { type: string, coordinates: [number] } }Pruebe el siguiente código, funciona para mí.
@index({ location: '2dsphere' }) export class GPSData extends Typegoose{ @prop({ required: true }) public log_Id!: mongoose.Types.ObjectId; @ValidateNested({each: true}) @prop({ _id : false }) readonly location: Location; } export class Location extends Typegoose{ @prop() @IsString({message: " type should be text"}) readonly type: String; @prop({ required: true }) coordinates: [[Number]]; }Colección de muestra (el tipo puede ser cualquiera de estos "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon" y "GeometryCollection"):
{ "log_Id": "5fbdec08ce02d61fec9a0189", "location":{ "type":"MultiPoint", "coordinates":[ [ -73.9580, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.9814, 40.7681 ] ] } }