Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

GeoJSON Support #45

Closed
gkTim opened this issue Oct 5, 2017 · 4 comments
Closed

GeoJSON Support #45

gkTim opened this issue Oct 5, 2017 · 4 comments

Comments

@gkTim
Copy link

gkTim commented Oct 5, 2017

Hi,

is it possible to use GeoJSON with Typegoose?
I'm not able to create an index so I can search it with $nearSphere.

What I've tried is to create the following model to store the data:

export class LocationData extends Typegoose {
    @prop()
    public type: string;

    @arrayProp({ items: Number })
    public coordinates: [number];
}

In my user model I have added the following attribute:

export class User extends Typegoose {
    @prop()
    public currentLocation: LocationData;
}

Since there must be an index on currentLocation to be able to perform the $nearSphere query I tried @prop({index: true}).

But I got the following error message:

TypeError: Undefined type `undefined` at `currentLocation.index`
  Did you try nesting Schemas? You can only nest using refs or arrays.

Some tutorials said you must define your own type to store the location data.
But how do I do it with Typegoose?

The mongoose way is:

var locationSchema = new Schema({  
  loc: {
    type: {
      type: "String",
      required: true,
      enum: ['Point', 'LineString', 'Polygon'],
      default: 'Point'
    },
    coordinates: [Number]
  }
});

locationSchema.index({ 'loc': '2dsphere' });
var Location = mongoose.model( 'Location', locationSchema );

What do I need to do to solve this issue?

@szokodiakos
Copy link
Owner

Hello. This could happen because of two reasons:

  • To define an index required for geoJSON you must use schema wise index definition (?). If this is the case, so no line index can be used (when defining the schema) then this is currently not supported by Typegoose, however schema-level index definition is a requested feature so it will likely be developed if I have the time (Support for compound indexes #43).
  • If it can be defined using prop-level index (like you did in the example), it is clearly a bug in Typegoose as the code should work.

Either way research must be made about geoJSON index (can it be defined in the schema property when using pure Mongoose). If it is possible then the bug must be eliminated in Typegoose.

Both researching and PRs are greatly appreciated, if you have the time. Otherwise when I'll have the time will look into it.

@hasezoey
Copy link
Contributor

hasezoey commented Jul 25, 2019

currently in the tests is

@index({ location: '2dsphere' })

and
@arrayProp({ items: Array })
public location: [[number]];

i dont know if this "fixes" the full issue here...

@naorzr
Copy link

naorzr commented Sep 10, 2019

@hasezoey honestly it works just fine for the queries OP specified.

defining class like in the example you brought

@index({ location: '2dsphere' })
export class Rating extends Typegoose {
  @prop({ ref: Car })
  public car: Ref<Car>;

  @prop({ ref: User })
  public user: Ref<User>;

  @prop()
  public stars: number;

  @arrayProp({ items: Array })
  public location: [[number]];
}

and doing query search with $nearSphere.

@hasezoey
Copy link
Contributor

i think this can now be closed as "solved"

@Ben305

@Ben305 Ben305 closed this as completed Sep 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants