使用php

时间:2016-01-14 06:26:25

标签: php mongodb search

我有这个mongodb系列。

{
"_id" : "3",
"SETTINGS" : {
    "PRIVACY" : {
        "ALLOW_SUGGESTIONS" : NumberLong(1),
        "BIRTHDAY" : NumberLong(0),
        "CONTACT_INFO" : {
            "PHONE" : NumberLong(0),
            "ADDRESS" : NumberLong(0)
        },
        "SEARCH_ENGINE" : NumberLong(0)
    }
}

我尝试使用php搜索PHONE。问题是我尝试的所有代码都取出了一个文档,然后我必须自己从文档中取出密钥PHONE

我的尝试是: -

$cursor=$collection->find(array('_id'=>'3'),array('SETTINGS'=>array('PRIVACY'=>array('CONTACT_INFO'=>array('PHONE')))));

foreach ($cursor as $document) { echo var_dump($document); }

它给了我输出: -

Fatal error: Uncaught exception 'MongoCursorException' with message 'localhost:27017: Can't canonicalize query: BadValue Unsupported projection option: SETTINGS: { PRIVACY: { CONTACT_INFO: [ "PHONE" ] } }' in /var/www/html/ProjectTest/include/Profile Helper.php:114 Stack trace: #0 /var/www/html/ProjectTest/include/Profile Helper.php(114): MongoCursor->rewind() #1 /var/www/html/ProjectTest/profile.php(14): Profile->GetProfileInfo() #2 {main} thrown in /var/www/html/ProjectTest/include/Profile Helper.php on line 114

总结我的问题: -

  • 有没有办法搜索这个特定的密钥,而不是整个文档。
  • 如果是,那么在大型数据库中考虑性能或搜索速度哪种方式会更好。

1 个答案:

答案 0 :(得分:2)

如果您提供了您想要的输出的代码示例以及您尝试过的代码,那会更容易。你可以尝试:

$db->$collection->find(array('SETTINGS.PRIVACY.CONTACT_INFO.PHONE' => 0))

此代码将为您提供电话号码为0

的文档

要仅退回手机,您必须使用projection

$db->$collection->find(array('SETTINGS.PRIVACY.CONTACT_INFO.PHONE' => 0), array('_id' => 0, 'SETTINGS.PRIVACY.CONTACT_INFO.PHONE' => 1))

编辑:根据您的问题版本,您缺少dot notation