在php中使用Mongo多$ elemMatch

时间:2016-02-15 11:57:15

标签: php mongodb

我需要帮助才能找到我在这里解决的问题,我需要在php中多次使用$ elemMatch,我需要和声明在多个阵列上,希望在这里有所帮助。

我的MongoDB json数组看起来像这样:

{
    "_id" : ObjectId("56c1770ad857b64e379af41c"),
    "UUID" : "75a29021-9cc2-4896-934b-471fc4c7c3da",
    "CNETID" : "S11589612",
    "Url" : "asus-stikadapter",
    "LastChanged" : "2016-02-04 11:18:13",
    "Manufacturer" : {
        "UUID" : "ca21b6aa-2ba1-11e5-a2ac-0050569068de",
        "Title" : "ASUS",
        "Url" : "asus",
        "SKU" : "0A200-00021100",
        "EAN" : null
    },
    "Store" : {
        "Title" : "ASUS stikadapter",
        "Description" : "",
        "Price" : "31900",
        "PriceDiscount" : "0",
        "AllowDisplay" : "1",
        "Activated" : "1",
        "LastSynced" : "1000-01-01 00:00:00"
    },
    "Category" : [ 
        "4afe0190-5391-4022-a6af-ceb0e6c2db87", 
        "347004bc-32e2-4988-868d-cb7a9242ab2d", 
        "bb2b92ea-75a7-4d41-99fc-0ba3239b811f", 
        "f6883831-2cc1-4e4a-987c-6eb78bcfacf1"
    ],
    "PhotoCDN" : [ 
        {
            "photoUUID" : "5fb98931-4626-11e5-a899-0050569068de",
            "isPrimary" : "1",
            "cdnPath" : "{path}"
        }
    ],
    "Specifications" : {
        "Small" : [ 
            {
                "Header" : "Produktbeskrivelse",
                "Body" : "ASUS stikadapter"
            }, 
            {
                "Header" : "Type",
                "Body" : "Stikadapter"
            }, 
            {
                "Header" : "Lokalisering",
                "Body" : "Storbritannien"
            }, 
            {
                "Header" : "Farve",
                "Body" : "Sort"
            }, 
            {
                "Header" : "Designet for",
                "Body" : "P/N: 0A001-00230000"
            }
        ],
        "Full" : [ 
            {
                "Header" : "Type",
                "Body" : "Stikadapter"
            }, 
            {
                "Header" : "Farve",
                "Body" : "Sort"
            }, 
            {
                "Header" : "Farvekategori",
                "Body" : "Sort"
            }, 
            {
                "Header" : "Lokalisering",
                "Body" : "Storbritannien"
            }, 
            {
                "Header" : "Designet for",
                "Body" : "P/N: 0A001-00230000"
            }
        ],
        "Search" : [ 
            {
                "HeaderID" : "T0000010",
                "Body" : "Stikadapter"
            }, 
            {
                "HeaderID" : "T0000087",
                "Body" : "Sort"
            }, 
            {
                "HeaderID" : "T0005917",
                "Body" : "Sort"
            }, 
            {
                "HeaderID" : "T0000046",
                "Body" : "Storbritannien"
            }, 
            {
                "HeaderID" : "T0003807",
                "Body" : "P/N: 0A001-00230000"
            }
        ]
    }
}

我想要搜索的内容是HeaderID和Body上的Specifications -> Full

我的问题我只能使用$elemMatch一次,我在php中使用find命令以及我想在其之后搜索的内容

查找匹配的所有产品

HeaderID = T0000087 and Body = Black, White or Yello AND
HeaderID = T0000087 and Body = UK, USA or DK

我的搜索过滤器看起来像这样,因为我现在测试它不能正常工作!

$this->_searchFilter = [
    'Category' => $this->categoryUUID,
    'Store.AllowDisplay' => '1',
    'Store.Activated' => '1',
    'Specifications.Search' => [
        ['$elemMatch' => [
            'HeaderID' => 'T0000010',
            'Body' => 'USB-kabel'
        ]],
        ['$elemMatch' => [
            'HeaderID' => 'T0000087',
            'Body' => 'Brunsort'
        ]]
    ]
];

我的调试MongoDB看起来像这样

db.getCollection('product').find({ "Specifications.Search": { $elemMatch: { 'HeaderID' : 'T0000010', 'Body' : "Stikadapter"}}})

1 个答案:

答案 0 :(得分:0)

我找到了awsare,我想要的是使用$和$ $和多$ elemMatch,我的代码看起来像这样。

db.getCollection('product').find({
    $and: [{ 
        $or :[{
            "Specifications.Search": { 
                $elemMatch: { 
                    'HeaderID' : 'T0000010', 
                    'Body' : "Stik"
                }
            }
        }, {
            "Specifications.Search": { 
                $elemMatch: { 
                    'HeaderID' : 'T0000010', 
                    'Body' : "Stikadapter"
                }
            }
        }]
    }]
})