Typegoose + nestjs + mongodb:索引不是使用@index 装饰器创建的
Typegoose + nestjs + mongodb: index not creating using @index decorator
我在使用 typegoose 时遇到错误。我有一个名为 SP
的模型,我必须在其名为 geoLocation
的 属性 上创建 2dsphere 索引 我尝试了 typegoose decorator @index
但即使它没有抛出任何错误也无法正常工作,我不知道发生了什么事以及 typegoose 如何处理它。有没有人能给我解决办法。
代码:
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]
}
}
试试下面适合我的代码
@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”之一):
{
"log_Id": "5fbdec08ce02d61fec9a0189",
"location":{
"type":"MultiPoint",
"coordinates":[
[ -73.9580, 40.8003 ],
[ -73.9498, 40.7968 ],
[ -73.9737, 40.7648 ],
[ -73.9814, 40.7681 ]
]
}
}
我在使用 typegoose 时遇到错误。我有一个名为 SP
的模型,我必须在其名为 geoLocation
的 属性 上创建 2dsphere 索引 我尝试了 typegoose decorator @index
但即使它没有抛出任何错误也无法正常工作,我不知道发生了什么事以及 typegoose 如何处理它。有没有人能给我解决办法。
代码:
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]
}
}
试试下面适合我的代码
@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”之一):
{
"log_Id": "5fbdec08ce02d61fec9a0189",
"location":{
"type":"MultiPoint",
"coordinates":[
[ -73.9580, 40.8003 ],
[ -73.9498, 40.7968 ],
[ -73.9737, 40.7648 ],
[ -73.9814, 40.7681 ]
]
}
}