Registry / database / mongoose-geojson-schema

mongoose-geojson-schema

JSON →
library3.1.1jsnpmunverified

Schema definitions for GeoJSON types (Point, MultiPoint, LineString, Polygon, etc.) for use with Mongoose JS. Current stable version is 3.1.1. Released under MIT license. Key differentiator: provides Mongoose schema types with built-in coordinate validation following the strict GeoJSON specification, defaulting to WGS84 datum. Available types include Geometry, GeometryCollection, Feature, and FeatureCollection. Supports both JavaScript and TypeScript. Requires Mongoose as a peer dependency. GitHub: echoes-xyz/mongoose-geojson-schema.

npm install mongoose-geojson-schema
INSTALL
IMPORT
SIG · MONGOOSE-GEOJSON-S
M
mongoose-geojson-schema
databasejavascriptv3.1.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mongoose (modified by side-effect)
const mongoose = require('mongoose'); require('mongoose-geojson-schema');
const mongoose = require('mongoose'); const GeoJSON = require('mongoose-geojson-schema');
require() modifies mongoose by side-effect; do not assign to variable.
Point
import { Point } from 'mongoose-geojson-schema';
import Point from 'mongoose-geojson-schema';
TypeScript: named export for type definitions.
Polygon
import { Polygon } from 'mongoose-geojson-schema';
const Polygon = require('mongoose-geojson-schema').Polygon;
Named export for both TS types and JS values.
Feature
import { Feature } from 'mongoose-geojson-schema';
import { Feature } from '@types/geojson';
Feature is a Mongoose schema type, not a general GeoJSON interface.

Creates a Mongoose schema with GeoJSON types (Point, Polygon, Feature) and validates a document with coordinate constraints.

import mongoose from 'mongoose'; import 'mongoose-geojson-schema'; const schema = new mongoose.Schema({ point: mongoose.Schema.Types.Point, polygon: mongoose.Schema.Types.Polygon, feature: mongoose.Schema.Types.Feature }); const Model = mongoose.model('GeoJSON', schema); const doc = new Model({ point: { type: 'Point', coordinates: [-73.97, 40.77] }, polygon: { type: 'Polygon', coordinates: [[ [-73.98, 40.76], [-73.96, 40.76], [-73.96, 40.78], [-73.98, 40.78], [-73.98, 40.76] ]] }, feature: { type: 'Feature', geometry: { type: 'Point', coordinates: [-73.97, 40.77] }, properties: {} } });
Debug
Known issues
gotchaCoordinates are validated to WGS84 (lon/lat) decimal degrees. Using coordinates outside -180/180 lon or -90/90 lat will fail validation.
fix
If you need a different CRS or no validation, set crs property to null or alternate per GeoJSON spec.
affects: >=1.0.0
gotchaPackage modifies Mongoose globally by side-effect. If you require() it after creating schemas, the types will not be available.
fix
Require() mongoose-geojson-schema before defining any schemas that use its types.
affects: >=3.0.0
deprecatedOld import patterns using require('mongoose-geojson-schema').Point (as a value) may not work in TypeScript or ESM.
fix
Use named imports: import { Point } from 'mongoose-geojson-schema' or import 'mongoose-geojson-schema'.
affects: >=3.0.0
gotchaPolygon coordinates require at least one ring (array of positions) and must be closed (first == last). Invalid arrays will throw validation errors.
fix
Ensure each ring is an array of coordinate arrays starting and ending with the same point.
affects: >=1.0.0
gotchaGeometries in Feature must include a 'geometry' property and 'properties' (can be null). Missing or malformed properties will fail validation.
fix
Always include 'properties: {}' or 'properties: null' when using Feature type.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'Point')
mongoose-geojson-schema not required yet; Mongoose schema types not registered.
fix
Add require('mongoose-geojson-schema'); before using mongoose.Schema.Types.Point
ValidationError: location: Cast to Point failed for value "{ type: 'Point', coordinates: [] }"
Empty coordinates array provided instead of [lon, lat] pair.
fix
Provide coordinates as an array of two numbers: [longitude, latitude].
TypeError: Cannot assign to read only property 'Point' of object '#<Object>'
Attempting to reassign mongoose.Schema.Types.Point after types are registered.
fix
Do not reassign; use mongoose.Schema.Types.Point directly.
TypeError: (intermediate value).Schema is not a constructor
Using mongoose after requiring mongoose-geojson-schema before mongoose is fully loaded.
fix
Ensure mongoose is required/imported before requiring mongoose-geojson-schema.
Upgrade
Version history
3.1.1latest on npm
Audit
Dependencies
mongooserequiredpeer dependency - modifies Mongoose schemas with GeoJSON types
Agent activity
9 hits · last 30 days
node
8
Resources
mongoose-geojson-schema — npm install mongoose-geojson-schema · libregistry