mongodb-schema
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
module.exports = require('./mongodb-schema.js'); | ||
module.exports = require('./lib/mongodb-schema.js'); |
{ | ||
"name": "mongodb-schema", | ||
"description": "Infer a probabilistic schema of javascript object, JSON documents or a MongoDB collection", | ||
"version": "0.0.2-alpha.1", | ||
"description": "Infer a probabilistic schema javascript objects, JSON documents or a MongoDB collection", | ||
"version": "0.0.2-alpha.2", | ||
"author": "Thomas Rueckstiess <thomas@rueckstiess.net>", | ||
@@ -6,0 +6,0 @@ "license": "Apache", |
mongodb-schema | ||
============== | ||
Extends the `DBCollection` object in the mongo shell to infer the schema of a MongoDB collection. | ||
Infer probabilistic schema of JSON documents, javascript objects or a MongoDB collection. | ||
This is an early prototype. Use at your own risk. | ||
This package is dual-purpose. It serves as a node.js module and can also be used with MongoDB directly, where it extends the `DBCollection` shell object. | ||
_mongodb-schema_ is an early prototype. Use at your own risk. | ||
<br> | ||
## Usage with Node.js | ||
### Installation | ||
Install the script with: | ||
Two ways to invoke the script. | ||
``` | ||
npm install mongodb-schema | ||
``` | ||
##### 1. Load the script directly (one-time usage) | ||
### Usage | ||
Then load the module and use it like this (optionally with an options object as second parameter): | ||
```js | ||
var schema = require('mongodb-schema'); | ||
// define some documents | ||
var documents = [ | ||
{a: 1}, | ||
{a: {b: "hello"}} | ||
]; | ||
// infer schema | ||
schema_obj = schema( documents, {flat: true} ); | ||
// pretty print | ||
console.log( JSON.stringify(schema_obj, null, '\t') ); | ||
``` | ||
<br> | ||
## Usage with MongoDB | ||
### Installation | ||
There are two ways to load the script, one-time (for testing) and permanent (for frequent use). | ||
#### 1. Load the script directly (one-time usage) | ||
This will load the `mongodb-schema.js` and the open the shell as usual. You will have to add the script every time you open the shell. | ||
``` | ||
mongo mongodb-schema.js --shell | ||
mongo <basepath>/lib/mongodb-schema.js --shell | ||
``` | ||
##### 2. Load the script via the `.mongorc.js` file (permanent usage) | ||
Replace the `<basepath>` part with the actual path where the `mongodb-schema` is located. | ||
#### 2. Load the script via the `.mongorc.js` file (permanent usage) | ||
You can also add the following line to your `~/.mongorc.js` file to always load the file on shell startup (unless started with `--norc`): | ||
```js | ||
load('/<path-to>/mongodb-schema.js') | ||
load('<basepath>/lib/mongodb-schema.js') | ||
``` | ||
Replace the `<path-to>` part with the actual path where the `mongodb-schema.js` is located. | ||
Replace the `<basepath>` part with the actual path where the `mongodb-schema` is located. | ||
@@ -44,3 +83,20 @@ | ||
Example of schema: | ||
##### Usage with options | ||
You can pass in an options object into the `.schema()` method. Currently it supports 2 options: `numSamples` and `flat`. | ||
```js | ||
db.foo.schema( {numSamples: 20, flat: true} ) | ||
``` | ||
This will use the first 20 documents to calculate the schema and return the schema as flat object (all fields are collapsed to the top with dot-notation). | ||
<br> | ||
## Example schemata | ||
#### Example of nested schema (default) | ||
100 documents were passed to the schema function (in MongoDB-mode, this is the default if the `numSamples` option is not specified). | ||
```json | ||
@@ -72,13 +128,6 @@ { | ||
##### Usage with options | ||
#### Example of flat schema | ||
You can pass in an options object into the `.schema()` method. Currently it supports 2 options: `numSamples` and `flat`. | ||
20 documents were passed to the schema function, as well as the option `{flat: true}`. | ||
```js | ||
db.foo.schema( {numSamples: 20, flat: true} ) | ||
``` | ||
This will use the first 20 documents to calculate the schema and return the schema as flat object (all fields are collapsed to the top with dot-notation). | ||
Example of _flat_ schema: | ||
```json | ||
@@ -85,0 +134,0 @@ { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22122
5.36%150
48.51%1
Infinity%