MongoDB/JS - 高级分面搜索 - 如何仅获取相关类别/值 [英] MongoDB/JS - Advanced Faceted search - How to get only relevant categories/values

查看:47
本文介绍了MongoDB/JS - 高级分面搜索 - 如何仅获取相关类别/值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先感谢您打开我的问题.在写实际问题之前,我会写一些与我的项目相关的细节,以充分了解我想要实现的目标.

First of all thank you for opening my question. Before I write the actual question I write some details related to my project to fully understand what I want to achieve.

我有产品系列.在这个集合中,每个文档都有不同的键和值(数据是动态的).在下面的示例中,前两个文档具有相同的键但值不同,第三个文档缺少键:'Color':

I have Product collection. In this collection each document has different keys and values (the datas are dynamic). On the example below the first two document has the same keys but the values are different, and the third document is missing the key:'Color':

{
_id:1
MainCategory:Vehicle
SubCategory:Car
Model:BMW
Color:Black
}

{
_id:2
MainCategory:Vehicle
SubCategory:Car
Model:Audi
Color:Red
}

{
_id:3
MainCategory:Vehicle
SubCategory:Car
Model:BMW
}

我有一个基于 MainCategory+SubCategory 的 Category 集合,在这里我存储键和值,这将用于提高过滤速度/体验.

I have a Category collection this is based on MainCategory+SubCategory and here I store the keys and values, this will be used for a better filtering speed/experience.

_id:..
MainCategory:Vehicle
SubCategory:Car
Fields:{
        Model: {BMW:[_id1,_id3], Audi:[_id2]},
        Color: {Black:[_id1],Red:[_id2]}
       }

我想根据类别集合创建一个过滤选项,其中所有字段"键都将是过滤类别,而这些键所具有的值将是过滤选项.上面的例子是初始过滤状态:

I want to make a filtering option based on Category collection, where all of the 'Fields' keys will be the filtering categories, and the values what these keys have will be the filtering options. The example above is the initial filtering state:

MainCategory-Vehicle
SubCategory-Car
Model - BMW, Audi
Color - Black, Red

现在是问题部分:如果用户选择 Model=BMW,我只想从我的 Category 集合中检索相关的类别和值.在这种情况下,相关意味着以下步骤:

And now here comes the question part: If a user selects the Model=BMW I want to retrieve only the relevant categories and values from my Category collection. In this case relevant means the following steps:

1.From Fields->Model->BMW 我想获取类别值为 BMW 的产品数组 ->我会得到 [_id1,_id3] .这将是我将在 UI 上显示的数据.

1.From Fields->Model->BMW I want to get the array of products which has the category value BMW -> i will get [_id1,_id3] . This will be the data which I will show on my UI.

高级部分:2.当我获得 ID 时,我想检查字段"中的其他对象是否包含上述列表中的任何产品:在这种情况下,字段-> 颜色-> 黑色包含 _id1

The Advanced Part: 2.When I get the IDs I want to check if other objects from 'Fields' are containing any of the products from the above list:in this case the Fields->Color->Black contains the _id1

3.如果一个对象包含我的产品之一,我想检索关键字作为过滤类别 ->颜色和包含它的值 ->红色

3.If an object is containing one of my products I want to retrieve the key as filtering category -> Color and the value which contained it -> Red

在此之后,我希望得到如下输出:

After this I will want to get the output something like this:

Products:[_id1,_id3]
Model:[BMW]
Color:[Black]

这样我的过滤将只显示相关的类别和值,看起来像这样:

By this my filtering will show only the relevant categories and values and will look like this:

MainCategory:Car
SubCategory:Vehicle
Model: BMW
Color:Black

示例 2另一个例子,这部分很难实现给定的输出是:如果用户选择 Color:Red 值,我想检索 _id2 产品并从模型中获取之前的奥迪"值.

Example 2 Another example and this part makes hard to achieve the given output is: If a user selects the Color:Red value I want to retrieve the _id2 product and get from the Model only the 'Audi' value previously.

所以在这种情况下,过滤看起来像:

So in this case the filtering will look like:

Product:[_id2]
Model:Audi
Color:Red

示例 3第三个问题可能是当用户选择多个过滤类别值时.例如,如果 BMW 有一个附加的 _id4,而 _id4 也出现在红色中.如果用户选择 Model:BMW 和 Color:Red 作为过滤选项,我们应该在最终输出中加入 BMW id 数组和 Red id 数组,以只返回 BMW 和 Red 选项.

Example 3 A third problem can be when a user selects more than one filtering category-value. For example if BMW has an addiotnal _id4, and _id4 is present in Color Red too. If a user selects Model:BMW and Color:Red for filtering option, we should join at the final output the BMW id array with the Red id array to get back only the BMW and Red options.

本例过滤:

Product [_id1,_id3,_id4]
Model:BMW
Color:Red

如何在我的对象中找到数组并检查该对象内部是否有任何数组值存在于其他数组中?是否可以只用一个查询来完成,或者最好进行两个查询?提前致谢.

How can I achieve to find the array in my object and check inside that object if any of the array values are present in other arrays? Is it possible to do it with only one query or it would be optimal to make two queries? Thanks in advance.

更新:只是一个额外的问题:这种数据结构是否适合类别收集,或者我应该考虑完全不同的结构?也许您可以建议一些其他方法来更轻松、更快速地处理数据?

Update: Just an additional question: Is this kind of data structuring is good for Category collection or maybe I should think in totally other structure? Maybe can you advice something else for handling easier and faster the data?

更新 2

//Product creation part -> the productbod contains the above mentioned key-value pairs

export const createProduct = async (productbod) => {
    try {
        if (!ObjectID.isValid(productbod.userId)) throw 'Invalid MongoDB ID.'
        const db=getDB()
        const product= db.collection('products')
    
        const finalProduct = await product.insertOne(productbod)
        return finalProduct
        
    } catch (err) {
        throw error
    }
}

推荐答案

在最基本的情况下,您可以创建如下 2 个类别

At the very basic, you can create the 2 categories as follows

Category
- _id
- category

Products
_ _id (the product _id )
- category (use the _id from Category)
- subcategory (use string, or if you want, create a subcategory collection)
- name
- attributes (color, model)

当您想查找所有类别时

db.category.find()

当您需要查找某个类别中的所有产品时,请使用 Category _id

When you need to find all products in a category, use the Category _id

db.products.find({ cateogry: _id})

当您需要查找某个类别和子类别中的所有产品时

When you need to find all products in a category, AND sub category

db.products.find({ category: _id, subcategory: 'salon' }) //or subcategory_id

当您需要查找单个产品时

When you need to find a single product

db.products.findOne({ _id }) //with the product_id

当您想按名称查找产品时

When you want to find a product by name

db.products.find({ name: { $regex : 'some search term', $option: 'i' } }) // where search term can be part of the product name

当您要查找包含 BMW 的所有类别时

When you want to find all categories that contains BMW

db.products.aggregate([
    {
       $match: { "attributes.model": "BMW"
     },
     {
        $group: { _id: "$category" }
      }
])

一旦您开始创建数据库,那么您就可以使用聚合以及 javascript 为您的数据库创建视图.(您可以创建一个单独的 stackoverflow 问题)

Once you have started creating the database, then you move on to creating the views for your db with aggregation, as well as javascript. (that you can create a separate stackoverflow question)

上述数据结构和示例查询应该足以处理您的高级过滤"

The above data structure and example queries should be sufficient to handle your "advanced filtering"

这篇关于MongoDB/JS - 高级分面搜索 - 如何仅获取相关类别/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆