简体   繁体   English

如何在 mongodb php 中封装多个 $in 查询

[英]How to encapsulate multiple $in queries in mongodb php

Here PRODUCT_NAME and BRAND_NAME are two different columns from same document.这里PRODUCT_NAMEBRAND_NAME是同一文档中的两个不同列。 And I want to apply a filter on the selected product and brand names.我想对选定的产品和品牌名称应用过滤器。

My array is something like this我的数组是这样的

Array
(
    [PRODUCT_NAME] => Array
        (
            [$and] => Array
                (
                    [$in] => Array
                        (
                            [0] => Product1
                            [1] => Product2
                        )

                )

        )

    [BRAND_NAME] => Array
        (
            [$and] => Array
                (
                    [$in] => Array
                        (
                            [0] => Brand1
                        )

                )

        )

)

The way to do this with mongodb ( assuming last version ) is to use mongodb aggregation and in particular $unwind使用 mongodb (假设是最新版本)执行此操作的方法是使用 mongodb 聚合,特别是$unwind

So as example in your case:所以作为你的例子:

db.yourcollection.aggregate( [
  { $unwind : "$firstattribute" } , // 1
  { $unwind : "$PRODUCT_NAME" },    // 2
  { $unwind : "$BRAND_NAME" },      // 3
] )

Now some explanation:现在做一些解释:

  1. because in your example, you provided an array, you have to unwind it, if is not so, please remove因为在您的示例中,您提供了一个数组,您必须展开它,如果不是这样,请删除
  2. here you unwind product name array在这里你展开产品名称数组
  3. and here brand name和这里的品牌名称

Note: I can provide a better (and runnable) solution with a real example ( a json with at least 2 elements in each array注意:我可以通过一个真实示例提供更好(且可运行)的解决方案(一个 json,每个数组中至少有 2 个元素

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

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