首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Typegoose + nestjs + mongodb:未使用@index修饰器创建索引

Typegoose + nestjs + mongodb:未使用@index修饰器创建索引
EN

Stack Overflow用户
提问于 2021-01-08 02:04:32
回答 1查看 598关注 0票数 1

我遇到了typegoose错误。我有一个名为SP的模型,我必须在它的属性上创建名为geoLocation的2dsphere索引我尝试了typegoose装饰器@index,但它不工作,甚至它没有抛出任何错误,我不知道发生了什么以及typegoose如何处理它。有没有人给我这个问题的解决方案?。

代码:

代码语言:javascript
复制
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]
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-14 00:24:04

试试下面的代码,它对我很有效

代码语言:javascript
复制
@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]];
}

示例集合(类型可以是"Point“、"MultiPoint”、"LineString“、"MultiLineString”、"Polygon“、"MultiPolygon”和“GeometryCollection”之一):

代码语言:javascript
复制
{ 
"log_Id": "5fbdec08ce02d61fec9a0189",

"location":{
    "type":"MultiPoint",
    "coordinates":[
 [ -73.9580, 40.8003 ],
 [ -73.9498, 40.7968 ],
 [ -73.9737, 40.7648 ],
 [ -73.9814, 40.7681 ]
 ]  
}
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65617696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档