# geojson-to-mongo

> A simple CLI tool that takes in a geoJSON feature collection and adds the features as documents to a mongo collection. Built using streams so it can handle HUGE files (think CENSUS BLOCKS for all of USA!).

Latest version **0.0.4** (published 2015-02-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install geojson-to-mongo
pnpm add geojson-to-mongo
yarn add geojson-to-mongo
bun add geojson-to-mongo
```

Provides the command `geojson-to-mongo`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2015-02-24 |
| First published | 2015-02-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Known vulnerabilities | 0 (+8 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Kevin Stephens |
| Maintainers | kevinmstephens |

## Links

- npm: https://www.npmjs.com/package/geojson-to-mongo
- Repository: https://github.com/kevinmstephens/mongoose-geojson
- Issues: https://github.com/kevinmstephens/mongoose-geojson/issues
- npm.io page: https://npm.io/package/geojson-to-mongo

## Dependencies (7)

- [hoek](https://npm.io/package/hoek.md) 2.11.0
- [async](https://npm.io/package/async.md) 0.9.0
- [lodash](https://npm.io/package/lodash.md) 3.3.0
- [mongodb](https://npm.io/package/mongodb.md) 1.4.31
- [commander](https://npm.io/package/commander.md) 2.6.0
- [JSONStream](https://npm.io/package/JSONStream.md) 0.10.0
- [event-stream](https://npm.io/package/event-stream.md) 3.2.2

## Recent versions

- 0.0.4 (latest) — 2015-02-24
- 0.0.3 — 2015-02-23
- 0.0.2 — 2015-02-22
- 0.0.1 — 2015-02-22

## README

# geojson-to-mongo

A simple CLI tool that takes in a geoJSON feature collection and adds the features as documents to a mongo collection. Built using streams so it can handle HUGE files (think CENSUS BLOCKS for all of USA!).

Also creates a `2dsphere` index on the `geometry` field of the geoJSON documents so you are ready for geospatial queries when it's done.

#### Motivation

Getting sick of moving CENSUS shapefiles to MongoDB and wanting to build a CLI tool to do that for me.
Shapefile input and simplification coming...

## Quick Start

```
npm install geojson-to-mongo
```

Input geoJSON file should contain `features` with array of valid geoJSON objects (this conforms to geoJSON feature collection).

```
{
  features: [
    {geoJSON object1},
    {geoJSON object2},
    ...
  ]
}
```

Execute the script:

The following will connect to the `tracts` db on the mongo instance at `localhost:27017` and add documents found at `./polygons.geojson`.

```
./node_modules/.bin/geojson-to-mongo --input ./polygons.geojson --uri mongodb://localhost:27017/tracts --collection polygons
```

### CLI Flags

See all of the CLI options.

`./node_modules/.bin/geojson-to-mongo --help`

```
    --input [path]       Required. Path to input geoJSON file
    --uri [uri]          Optional. Default "mongodb://localhost:27017/test"
    --collection [name]  Required. Collection to write to
    --drop-collection    Optional. Default false. Drop the collection before insertions
```

### Mongoose Example

This example schema and schema method will work the `tracts` collection.

```
var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var geoJSONSchema = new Schema({
  geometry: {
    type: {type: String},
    coordinates: []
  }
}, {
  collection: "tracts"
});

// Search for document intersecting lat/lng and return ONE.
// options.lat
// options.lng
geoJSONSchema.statics.findByLatLng = function (options, callback) {
  var lat = options.lat;
  var lng = options.lng;

  var query = {
    "geometry": {
      "$geoIntersects": {
        "$geometry": {
          type: "Point",
          coordinates: [options.lng, options.lat]
        }
      }
    }
  };

  this.findOne(query, callback);
};

module.exports = mongoose.model("tracts", geoJSONSchema);
```

---
_Source: https://npm.io/package/geojson-to-mongo · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
