簡體   English   中英

從PHP的另一個數組中查找MongoDB數組中的子字符串

[英]Find substring in MongoDB array from another array from PHP

我在Mongo中保存了一個更復雜的文檔“模式”,但是我需要匹配的部分看起來像這樣

"tags" : [
    {
        "tag" : "accompong maroon festival",
        "type" : "label"
    },
    {
        "tag" : "jamaica",
        "type" : "label"
    },
    {
        "tag" : "maroon warrior",
        "type" : "label"
    },
    {
        "tag" : "maroons",
        "type" : "label"
    },
    {
        "tag" : "caribbean culture",
        "type" : "label"
    },
    {
        "tag" : "rum",
        "type" : "label"
    }
}

我正在使用PHP查詢Mongo數據庫,並且不得不針對可能的單詞數組查詢每個文檔。

array(
    'boxing',
    'warrior'
)

我不知道如何編寫代碼以嘗試將我擁有的數組與保存在Mongo中的數據集進行匹配。 現在,我僅嘗試查看標簽是否在單詞數組內

$data = $this->event_model->find_by(
            array
            (
                'tags.tag' => array
                (
                    '$in' => $Words
                ),
                'published' => 'y'
            )
        );

通過首先創建一個使用MongoRegex搜索標簽的數組並將此數組添加到$ or過程中,我已經解決了此問題

        $or_array = array();
        foreach($Words as $w)
        {
            $or_array[] = array(
                'tags.tag' => new MongoRegex('/.*'. $w .'.*/i')
            );
        }

        $data = $this->event_model->find_by(
            array
            (
                '$or' => $or_array,
                'published' => 'y'
            )
        );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM