简体   繁体   中英

How to create Schema using mongodb(mongoose schema)?

I have Mobile List with models and rates. I want to create a schema for this. I am new to mongodb and mongoose. Anyone help me out, I have added my requirements.

Categories :

nokia

sub Categories :Nokia Lumia 730 -7,000,
                Nokia 225 -5,000,
                Nokia Lumia 1020 -6,000,
                Nokia Lumia 530 -8,0000

Samsung Galaxy A7:
                Samsung Galaxy A7 -10,000,
                Samsung Galaxy A3 -12,000,
                Samsung Galaxy One5 -5,000,
                Samsung Galaxy S5 Neo -6,000


HTC One M9s:

                HTC One M9s -9,000,
                HTC Desire 728G -12,000,
                HTC Desire 526 -4,000,

My Expectations: How can I design schema to solve below condition

  1. When I search nokia it should display nokia mobile model with rates.
  2. When I search nokia with Nokia Lumia, the result should show matched conditions

This is my full schema

var ShopSchema = new Schema({

    Email: {
        type: String,
        default: '',
        trim: true,

    },
    Storename: {
        type: String,
        default: '',
        trim: true
    },
    Type: {
        type: String,
        default: '',
        trim: true
    },
    Categories: {
        type: String,
        default: '',
        trim: true
    }


});

You can make two different collection for shop and categories

and make schema in nested way

var Categories = new Schema({
    name     : String
  , subcategories      : {
    name : String,
    model : String
     }
});


var ShopSchema = new Schema({

    Email: {
        type: String,
        default: '',
        trim: true,

    },
    Storename: {
        type: String,
        default: '',
        trim: true
    },
    Type: {
        type: String,
        default: '',
        trim: true
    },
    Categories : [Categories]
});

To understand nested schema in mongoose you can visit this link

I am giving an example to understand more-

Fist of all we have to save store name

{
    "_id" : ObjectId("567a8f004cb6178545bac89c"),
    "Email" : "dineshaws@hotmail.com",
    "Storename" : "Dineshaws",
    "Type" : "XYZ",
    "Categories" : []
}

When we'll save single category then it will go into both collection- categories and store

shop collection -

{
    "_id" : ObjectId("567a8f004cb6178545bac89c"),
    "Email" : "dineshaws@hotmail.com",
    "Storename" : "Dineshaws",
    "Type" : "XYZ",
    "Categories" : [
            {
                "_id" : ObjectId("567a9be04cb6178545bac89d"),
                "name" : "nokia",
                "subcategories" : []
            }
                   ]
}

categories collection -

{
    "_id" : ObjectId("567a9be04cb6178545bac89d"),
    "name" : "nokia",
    "subcategories" : []
}

Now if we want to insert subcategory than it will go into both collection as a sub document

categories collection will be look like below-

{
    "_id" : ObjectId("567a9be04cb6178545bac89d"),
    "name" : "nokia",
    "subcategories" : [
            {
                "name" : "Nokia Lumia",
                "model":"Nokia Lumia 730 -7,000"
            }
            ]
}

shop collection will be look like this-

{
    "_id" : ObjectId("567a8f004cb6178545bac89c"),
    "Email" : "dineshaws@hotmail.com",
    "Storename" : "Dineshaws",
    "Type" : "XYZ",
    "Categories" : [
            {
                "_id" : ObjectId("567a9be04cb6178545bac89d"),
                "name" : "nokia",
                "subcategories" : [{
                            "name" : "Nokia Lumia",
                            "model":"Nokia Lumia 730 -7,000"
                        }
                        ]
            }
                   ]
}

You can also change schema according your requirement

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM