how to define location in an entity using mongodb

泪湿孤枕 提交于 2020-06-01 05:07:07

问题


I am using nest js in my sample application. I define the entity of my application .Entity means a document. But I struck at one place to define the location.

using mongoose I define the schema of my document like this see link

https://raw.githubusercontent.com/webmakaka/Node.js-API-Masterclass-With-Express-MongoDB/master/api/models/Bootcamp.js

location: {
      // GeoJSON Point
      type: {
        type: String,
        enum: ['Point']
      },
      coordinates: {
        type: [Number],
        index: '2dsphere'
      },
      formattedAddress: String,
      street: String,
      city: String,
      state: String,
      zipcode: String,
      country: String
    },
    careers: {
      // Array of strings
      type: [String],
      required: true,
      enum: [
        'Web Development',
        'Mobile Development',
        'UI/UX',
        'Data Science',
        'Business',
        'Other'
      ]
    },

same thing I want to do using typeorm using mongoDB without mongoos .can you please help me how I will do that.

here is my entity class

import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, ManyToOne, ObjectID, ObjectIdColumn } from 'typeorm';
import { IsNotEmpty, MaxLength } from 'class-validator';

@Entity('bootcamp')
export class BootcampEntity extends BaseEntity {
  @ObjectIdColumn() id: ObjectID;

  @Column({type:'text',unique:true,length:50,nullable: false})
  name:string;

  @Column({type:'text'})
  slug: string;

  @Column({type:'text',length:500,nullable: false})
  description: string;

  @Column({type:'text'})
  website: string;

  @Column({type:'text',length:20})
  phone: string;

  @Column({type:'text',})
  email: string;


  @Column({type:'text',nullable: false})
  address: string;

  @Column({type:'text',array: true })
  careers: string[];

  @Column({type:'int'})
  averageRating:number

  @Column({type:'int'})
  averageCost:number
  //
  @Column({type:'string',default:'no-photo.jpg'})
  photo: string;
  //
  @Column({type:'boolean',default:false})
  housing: boolean;

  @Column({type:'boolean',default:false})
  jobAssistance: boolean;

  @Column({type:'boolean',default:false})
  jobGuarantee: boolean;

  @Column({type:'boolean',default:false})
  acceptGi: boolean;

  @Column({type:'date',default:Date.now()})
  createdAt: Date;

}

I am using this framework

https://docs.nestjs.com/

typeorm link https://typeorm.io/#/entities

there is schema

https://raw.githubusercontent.com/webmakaka/Node.js-API-Masterclass-With-Express-MongoDB/master/api/models/Bootcamp.js

I want make same schema using typeorm .I struck only defining the location attribute ?

how to define ?could you please tell me how I will define location in my entity class ?


回答1:


I think location would have similar structure to code below.

source 1 source 2

enum GeoJSONPoint {
  Point = "Point"
}

enum Careers {
  WebDevelopment = 'Web Development',
  MobileDevelopment = 'Mobile Development',
  UIUX = 'UI/UX',
  DataScience = 'Data Science',
  Business = 'Business',
  Other = 'Other'
}

@Entity('location')
export class LocationEntity extends BaseEntity {

  @Column({
    type: "enum",
    enum: GeoJSONPoint
  })
  type: GeoJSONPoint;

  @Column({type:'int'})
  coordinates: number;

  @Column({type:'text'})
  formattedAddress: string;

  @Column({type:'text'})
  street: string;

  @Column({type:'text'})
  city: string;

  @Column({type:'text'})
  state: string;

  @Column({type:'text'})
  zipcode: string;

  @Column({type:'text'})
  country: string;

  @Column({type:'simple-array'})
  careers: Careers[];
}


来源:https://stackoverflow.com/questions/62099379/how-to-define-location-in-an-entity-using-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!