【问题标题】:MongoDB create product summary collectionMongoDB 创建产品汇总集合
【发布时间】:2018-07-14 02:10:10
【问题描述】:

假设我有一个这样的产品集合:

{
"_id": "5a74784a8145fa1368905373",
"name": "This is my first product",
"description": "This is the description of my first product",
"category": "34/73/80",
"condition": "New",
"images": [
    {
        "length": 1000,
        "width": 1000,
        "src": "products/images/firstproduct_image1.jpg"
    },
    ...
],

"attributes": [
    {
        "name": "Material",
        "value": "Synthetic"
    },
    ...
],

"variation": {
    "attributes": [
        {
            "name": "Color",
            "values": ["Black", "White"]
        },
        {
            "name": "Size",
            "values": ["S", "M", "L"]
        }
    ]
}
}

还有一个像这样的变体集合:

{
"_id": "5a748766f5eef50e10bc98a8",
"name": "color:black,size:s",
"productID": "5a74784a8145fa1368905373",
"condition": "New",
"price": 1000,
"sale": null,
"image": [
    {
        "length": 1000,
        "width": 1000,
        "src": "products/images/firstvariation_image1.jpg"
    }
],
"attributes": [
    {
        "name": "Color",
        "value": "Black"
    },
    {
        "name": "Size",
        "value": "S"
    }
]
}

我想将文档分开,并且为了便于浏览、搜索和分面搜索实现,我想在一个查询中获取所有数据,但我不想加入我的应用程序代码。 我知道使用名为 summary 的第三个集合是可以实现的,它可能看起来像这样:

{
"_id": "5a74875fa1368905373",
"name": "This is my first product",
"category": "34/73/80",
"condition": "New",
"price": 1000,
"sale": null,
"description": "This is the description of my first product",
"images": [
    {
        "length": 1000,
        "width": 1000,
        "src": "products/images/firstproduct_image1.jpg"
    },
    ...
],

"attributes": [
    {
        "name": "Material",
        "value": "Synthetic"
    },
    ...
],

"variations": [
    {
        "condition": "New",
        "price": 1000,
        "sale": null,
        "image": [
            {
                "length": 1000,
                "width": 1000,
                "src": "products/images/firstvariation_image.jpg"
            }
        ],
        "attributes": [
            "color=black",
            "size=s"
        ]
    },
    ...
]
}

问题是,我不知道如何使摘要集合与产品和变体集合保持同步。我知道它可以使用 mongo-connector 来完成,但我不确定如何实现它。 请帮帮我,我还是个初级程序员。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您实际上不需要维护摘要集合,将产品和变体摘要存储在另一个集合中是多余的

    您可以使用聚合管道 $lookup 来代替使用 productID 外部连接产品和变体

    聚合管道

    db.products.aggregate(
        [
            {
                $lookup : {
                    from : "variation",
                    localField : "_id",
                    foreignField : "productID",
                    as : "variations"
                }
            }
        ]
    ).pretty()
    

    【讨论】:

    • 谢谢你,你的回答有帮助......但是如果我想像这里提到的那样维护那个摘要集合怎么办:dzone.com/articles/product-catalog-part-1-schema
    • @s.frz,老兄,你有没有找到任何方法让摘要文档与其他收藏保持同步?我期待您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 2021-12-11
    • 2020-10-25
    • 2017-10-19
    • 2017-12-10
    • 2021-10-27
    • 1970-01-01
    相关资源
    最近更新 更多