1

我有带有型号和费率的移动列表。我想为此创建一个模式。我是 mongodb 和 mongoose 的新手。任何人都可以帮助我,我已经添加了我的要求。

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,

我的期望:我如何设计模式来解决以下情况

  1. 当我搜索诺基亚时,它应该显示带有费率的诺基亚手机型号。
  2. 当我用诺基亚 Lumia 搜索诺基亚时,结果应该显示匹配的条件

这是我的完整架构

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
    }


});
4

1 回答 1

0

您可以为商店和类别制作两个不同的集合

并以嵌套方式制作模式

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]
});

要了解 mongoose 中的嵌套模式,您可以访问此链接

我举一个例子来了解更多-

首先,我们必须保存商店名称

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

当我们保存单个类别时,它将进入收藏类别和存储

店铺收藏——

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

类别集合 -

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

现在,如果我们要插入子类别,它将作为子文档进入两个集合

类别集合将如下所示 -

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

商店收藏将如下所示-

{
    "_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"
                        }
                        ]
            }
                   ]
}

您还可以根据您的要求更改架构

谢谢

于 2015-12-22T12:49:06.333 回答