简体   繁体   English

PHP和MongoDB查询-获取文档中的通用值列表

[英]PHP & MongoDB query - get list of common values across documents

I'm currently working on an e-commerce solution of my own. 我目前正在开发自己的电子商务解决方案。

First, a product document look like that - 首先,产品文档如下所示-

{
    "_id" : ObjectId("...."),
    "sku" : "",
    "manufacturer" : "", // Manufacturer
    "quantity" : 0,
    "price" : 0,
    "currency" : "", // Currency code
    "warranty" : "", // Yes/No/True/False
    "installation" : "", // Installiton type/required
    "images" : [], // array of img paths
    "creation" : ISODate("...."),
    "lastModified" : ISODate("...."),
    "details" : [], // array of object each for different language
    "properties" : [ 
        {
            "property" : "", // property name, unique
            "value" : "" // propery value
        }
    ],
    "customize" : [ 
        {
            "property" : "", // property name
            "type" : "", // input type: radio, select, etc.
            "options" : [ 
                {
                    "id" : 1, // numeric id
                    "option" : "", // option text
                    "cost" : 0.0 // option additional price
                }
            ]
        }
    ],
    "categories" : [], // array of objects {category:1, subs[2,3,4]}
    "excludes" : [],
    "shipping" : [], // array of objects {location:FR, price:5, additional: 1}
}

So, what I want to be able to do is to get a list of filters (max of 5, the most common). 因此,我想做的是获取过滤器列表(最多5个,最常见)。

Filters I need: 我需要的过滤器:

  1. simple filters - all values available for manufacturer + count for each 简单的过滤器-制造商可用的所有值+每个的计数
  2. nested filters - all values available for array_of_obj.x + count (example: properties.property) 嵌套过滤器-所有可用于array_of_obj.x的值+计数(例如:properties.property)
  3. even more nested for the customize array and categories array, but it will be just a bonus. 对于customize数组和Categories数组而言,嵌套更多,但这将是一个好处。

How can I achieve each one? 如何实现每个目标? Will it be too heavy? 会不会太重?

In case the query will be too complex, will it be better to get all products to an array of products (I'm using setTypeMap in order to unserialize the data directly to my php class) and then play with array_column, foreach etc.? 如果查询太复杂,最好将所有产品都放入一个产品数组(我正在使用setTypeMap以便将数据直接反序列化到我的php类中),然后使用array_column,foreach等玩吗?

Thanks to Neil Lunn, I managed to solve my problem by using $facet aggregation pipeline stage, even with the complex structure of an array => object => array . 感谢Neil Lunn,即使使用array => object => array的复杂结构,我也通过使用$facet聚合管道阶段解决了我的问题。

I'll post my code with a full explanation soon, in order to help others here. 我将在不久的将来发布带有完整说明的代码,以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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