首页 > 解决方案 > Mongo查找值中包含撇号的字符串

问题描述

字段值在 mongo 集合字段中包含一个带有撇号“Dreams I'll Never See”的字符串recording.title

$updateResult = $collection->findOneAndUpdate(
        [
            'recording-id'          => $out['recording']->id
        ],
        ['$set'  => [ 
            'recording'         => [
                'id'            => $out['recording']->id,
                'title'         => $out['recording']->title,
                'score'         => $out['recording']->score,
                'length'        => $out['recording']->length,
                'release-count' => count($out['recording']->releases)
                ]
        ],
        ['upsert' => true]);

当一个不存在时,这会在我的集合中正确创建我的记录,并且在再次运行时不会创建几个新记录,但是当我搜索标题“我永远不会看到的梦想”时,它找不到匹配项。

我的搜索代码示例:

$t = "Dreams I'll Never See";
$a = "Molly Hatchet";

$cursor = $collection->find(
                    ['$and' => [
                        [ 'recording.title'  =>  $t ],
                        [ 'artist.name'       => $a ]
                    ]
                    ],
                    ['projection'   => [
                        'recording'     =>1,
                        'release'       =>1,
                        'artist'        =>1,
                        'release-group' =>1
                    ],
                    ['limit' => 1]
                ]
    );

我整个早上都在谷歌上搜索,但似乎无法正常工作。

Mongo shell 显示存储的记录字段:

"recording" : {
        "id" : "446ff065-a9bb-4171-a0d9-dbfa29661f99",
        "title" : "Dreams I’ll Never See",
        "score" : 100,
        "length" : 428000,
        "release-count" : 13
    },

标签: phpmongodb

解决方案


推荐阅读