node.js - Typegoose + nestjs + mongodb:索引未使用@index 装饰器创建
问题描述
我遇到了 typegoose 的错误。我有一个名为的模型SP
,我必须在其属性上创建 2dsphere 索引,名为geoLocation
我尝试过 typegoose 装饰器@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 ]
]
}
}
推荐阅读
- python - 如何使用python将负数分解为其成分?
- python - 为我的数据框代码不断获取“TypeError:'numpy.ndarray'对象不可调用”
- http - 即使 curl 请求有效,Axios 和邮递员请求也不起作用
- css - 用于 Wordpress 主题的带有 CSS 的垂直弹出菜单
- python - 如何在文件中找到所有字符串子字符串然后对其进行操作?- Python
- redis - --scan --pattern *some_numbers* 找不到我的大部分密钥
- sql - MS Access:为什么我的错误信息在输入后立即出现?
- github-actions - 如何在 github 操作中引用环境机密
- node.js - mongoose.ObjectId 和 Schema.Types.ObjectId 的区别?
- python - 如何使用 conda develop 安装具有所有依赖项的脚本(在开发/可编辑模式下)?