
Security News
libxml2 Maintainer Ends Embargoed Vulnerability Reports, Citing Unsustainable Burden
Libxml2’s solo maintainer drops embargoed security fixes, highlighting the burden on unpaid volunteers who keep critical open source software secure.
mongodb-schema
Advanced tools
Infer a probabilistic schema javascript objects, JSON documents or a MongoDB collection
Infer probabilistic schema of javascript objects or a MongoDB collection.
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.
Install the script with:
npm install mongodb-schema
Then load the module and use call schema( documents, options, callback )
, which will call callback(err, res)
with an error or the result once it's done analysing the documents.
var schema = require('mongodb-schema');
// define some documents
var documents = [
{a: 1},
{a: {b: "hello"}}
];
// define options
var options = {flat: true};
// define callback function
var callback = function(err, res) {
// handle error
if (err) {
return console.error( err );
}
// else pretty print to console
console.log( JSON.stringify( res, null, '\t' ) );
}
// put it all together
schema( documents, options, callback );
This would output:
{
"$count": 2,
"a": {
"$count": 2,
"$type": {
"number": 1,
"object": 1
},
"$prob": 1
},
"a.b": {
"$count": 1,
"$type": "string",
"$prob": 0.5
}
}
There are two ways to load the script, one-time (for testing) and permanent (for frequent use).
This will first load mongodb-schema.js
and the open the shell as usual. You will have to add the script every time you open the shell.
mongo <basepath>/lib/mongodb-schema.js --shell
Replace the <basepath>
part with the actual path where the mongodb-schema
folder is located.
.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
):
load('<basepath>/lib/mongodb-schema.js')
Replace the <basepath>
part with the actual path where the mongodb-schema
folder is located.
The script extends the DBCollection
object to have another new method: .schema()
. On a collection called foo
, run it with:
db.foo.schema()
This will use the first 100 (by default) documents from the collection and calculate a probabilistic schema based on these documents.
You can pass in an options object into the .schema()
method. Currently it supports 2 options: samples
and flat
.
db.foo.schema( {samples: 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). See the Examples section below for nested vs. flat schemata.
The schema generated by this method annotates most of the fields in the object with schema information. These can be counts ($count
), type information ($type
), a flag whether or not the field was an array in at least one instance ($array
) and the probability of a field appearing given it's parent field ($prob
).
100 documents were passed to the schema function (in MongoDB-mode, this is the default if the samples
option is not specified).
{
"$c": 100,
"_id": {
"$count": 100,
"$prob": 1,
"$type": "objectid"
},
"a": {
"$count": 100,
"$prob": 1,
"b": {
"$count": 100,
"$prob": 1,
"$type": "number"
},
"c": {
"$count": 70,
"$prob": 0.7,
"$type": "string"
}
}
}
20 documents were passed to the schema function, as well as the option {flat: true}
.
{
"$count": 20,
"_id": {
"$count": 20,
"$prob": 1,
"$type": "objectid"
},
"a.b": {
"$count": 20,
"$prob": 1,
"$type": "number"
},
"a.c": {
"$count": 13,
"$prob": 0.65,
"$type": "string"
}
}
FAQs
Infer the probabilistic schema for a MongoDB collection.
The npm package mongodb-schema receives a total of 41,655 weekly downloads. As such, mongodb-schema popularity was classified as popular.
We found that mongodb-schema demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 26 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Libxml2’s solo maintainer drops embargoed security fixes, highlighting the burden on unpaid volunteers who keep critical open source software secure.
Research
Security News
Socket investigates hidden protestware in npm packages that blocks user interaction and plays the Ukrainian anthem for Russian-language visitors.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.