Home:ALL Converter>Generate a JSON schema from an existing MongoDB collection

Generate a JSON schema from an existing MongoDB collection

Ask Time:2020-09-07T21:42:03         Author:kaan_a

Json Formatter

I have a MongoDB collection that contains a lot of documents. They are all roughly in the same format, though some of them are missing some properties while others are missing other properties. So for example:

[
  {
    "_id": "SKU14221",
    "title": "Some Product",
    "description": "Product Description",
    "salesPrice": 19.99,
    "specialPrice": 17.99,
    "marketPrice": 22.99,
    "puchasePrice": 12,
    "currency": "USD",
    "color": "red",
  },
  {
    "_id": "SKU14222",
    "title": "Another Product",
    "description": "Product Description",
    "salesPrice": 29.99,
    "currency": "USD",
    "size": "40",
  }
]

I would like to automatically generate a schema from the collection. Ideally it would not which properties are present in all the documents and mark those as required. Detecting unique columns would also be nice, though not really all that necessary. In any event I would be modifying the schema after it's automatically generated.

I've noticed that there are tools that can do this for JSON. But short of downloading the entire collection as JSON, is it possible to do this using the MongoDb console or a CLI tool directly from the collection?

Author:kaan_a,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63778774/generate-a-json-schema-from-an-existing-mongodb-collection
yy