How to create the mongodb schema for below Json?

450 Views Asked by At
{
    "_id" : ObjectId("5a0e77f4b7368f14c088f542"),
    "folderName" : "team 4",
    "tag" : "search",
    "ismainFolder" : true,
    "innerFolder" : [
                    {
                       parentfolderId" : null,
                      "ismainFolder" : false,
                      "foldername" : "Onkar 11"
                        "subinnerFolder" : [
                                        {
                                            "parentfolderId" : null,
                                            "ismainFolder" : false,
                                            "foldername" : "Onkar 11"
                                            "thirdSubFolder" : [
                                                        {
                                                            "parentfolderId" : null,
                                                            "ismainFolder" : false,
                                                           "foldername" : "Onkar 11"
                                                        }, 
                                                        {
                                                             "parentfolderId" : null,
                                                             "ismainFolder" : false,
                                                             "foldername" : "Onkar 11"
                                                        }
                                                    ] 
                                        }, 

                                   ]
                    },
                ]
}

I need to create the mongodb schema for below json format so that innerFolder, subinnerFolder, etc (inner folder structure goes nested) so please help me in this ?

2

There are 2 best solutions below

2
TGrif On BEST ANSWER

Here is an example of schema in Mongoose style.
The only difficulty here is with null value.
One of your possibility is to store it as text, but consider to do not store null value at all.

const detailFolder = {
  parentfolderId: { type: String, default: null },
  ismainFolder: Boolean,
  foldername: String
}

const schema = {
  folderName: String,
  tag: String,
  ismainFolder: Boolean,
  innerFolder: [{
    detailFolder,
    subinnerFolder: [{
      detailFolder,
      thirdSubFolder: [ detailFolder ]
    }]
  }]
}
1
Sello Mkantjwa On

Below is a json schema generated by https://jsonschema.net/#/. You can edit it as required. For Mongoose schema, take a look at https://github.com/topliceanu/mongoose-gen

{
  "definitions": {}, 
  "$schema": "http://json-schema.org/draft-06/schema#", 
  "$id": "http://example.com/example.json", 
  "type": "object", 
  "properties": {
    "folderName": {
      "$id": "/properties/folderName", 
      "type": "string", 
      "title": "The Foldername Schema.", 
      "description": "An explanation about the purpose of this instance.", 
      "default": "", 
      "examples": [
        "team 4"
      ]
    }, 
    "tag": {
      "$id": "/properties/tag", 
      "type": "string", 
      "title": "The Tag Schema.", 
      "description": "An explanation about the purpose of this instance.", 
      "default": "", 
      "examples": [
        "search"
      ]
    }, 
    "ismainFolder": {
      "$id": "/properties/ismainFolder", 
      "type": "boolean", 
      "title": "The Ismainfolder Schema.", 
      "description": "An explanation about the purpose of this instance.", 
      "default": false, 
      "examples": [
        true
      ]
    }, 
    "innerFolder": {
      "$id": "/properties/innerFolder", 
      "type": "array", 
      "items": {
        "$id": "/properties/innerFolder/items", 
        "type": "object", 
        "properties": {
          "parentfolderId": {
            "$id": "/properties/innerFolder/items/properties/parentfolderId", 
            "type": "null", 
            "title": "The Parentfolderid Schema.", 
            "description": "An explanation about the purpose of this instance.", 
            "default": null, 
            "examples": [
              null
            ]
          }, 
          "ismainFolder": {
            "$id": "/properties/innerFolder/items/properties/ismainFolder", 
            "type": "boolean", 
            "title": "The Ismainfolder Schema.", 
            "description": "An explanation about the purpose of this instance.", 
            "default": false, 
            "examples": [
              false
            ]
          }, 
          "foldername": {
            "$id": "/properties/innerFolder/items/properties/foldername", 
            "type": "string", 
            "title": "The Foldername Schema.", 
            "description": "An explanation about the purpose of this instance.", 
            "default": "", 
            "examples": [
              "Onkar 11"
            ]
          }, 
          "subinnerFolder": {
            "$id": "/properties/innerFolder/items/properties/subinnerFolder", 
            "type": "array", 
            "items": {
              "$id": "/properties/innerFolder/items/properties/subinnerFolder/items", 
              "type": "object", 
              "properties": {
                "parentfolderId": {
                  "$id": "/properties/innerFolder/items/properties/subinnerFolder/items/properties/parentfolderId", 
                  "type": "null", 
                  "title": "The Parentfolderid Schema.", 
                  "description": "An explanation about the purpose of this instance.", 
                  "default": null, 
                  "examples": [
                    null
                  ]
                }, 
                "ismainFolder": {
                  "$id": "/properties/innerFolder/items/properties/subinnerFolder/items/properties/ismainFolder", 
                  "type": "boolean", 
                  "title": "The Ismainfolder Schema.", 
                  "description": "An explanation about the purpose of this instance.", 
                  "default": false, 
                  "examples": [
                    false
                  ]
                }, 
                "foldername": {
                  "$id": "/properties/innerFolder/items/properties/subinnerFolder/items/properties/foldername", 
                  "type": "string", 
                  "title": "The Foldername Schema.", 
                  "description": "An explanation about the purpose of this instance.", 
                  "default": "", 
                  "examples": [
                    "Onkar 11"
                  ]
                }, 
                "thirdSubFolder": {
                  "$id": "/properties/innerFolder/items/properties/subinnerFolder/items/properties/thirdSubFolder", 
                  "type": "array", 
                  "items": {
                    "$id": "/properties/innerFolder/items/properties/subinnerFolder/items/properties/thirdSubFolder/items", 
                    "type": "object", 
                    "properties": {
                      "parentfolderId": {
                        "$id": "/properties/innerFolder/items/properties/subinnerFolder/items/properties/thirdSubFolder/items/properties/parentfolderId", 
                        "type": "null", 
                        "title": "The Parentfolderid Schema.", 
                        "description": "An explanation about the purpose of this instance.", 
                        "default": null, 
                        "examples": [
                          null
                        ]
                      }, 
                      "ismainFolder": {
                        "$id": "/properties/innerFolder/items/properties/subinnerFolder/items/properties/thirdSubFolder/items/properties/ismainFolder", 
                        "type": "boolean", 
                        "title": "The Ismainfolder Schema.", 
                        "description": "An explanation about the purpose of this instance.", 
                        "default": false, 
                        "examples": [
                          false
                        ]
                      }, 
                      "foldername": {
                        "$id": "/properties/innerFolder/items/properties/subinnerFolder/items/properties/thirdSubFolder/items/properties/foldername", 
                        "type": "string", 
                        "title": "The Foldername Schema.", 
                        "description": "An explanation about the purpose of this instance.", 
                        "default": "", 
                        "examples": [
                          "Onkar 11"
                        ]
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}