【问题标题】:PHP Mongodb create Text IndexPHP Mongodb 创建文本索引
【发布时间】:2015-11-08 04:58:06
【问题描述】:

我是 MongoDB 的新手,我正在尝试创建一个文本索引。 尝试了几个小时后,我什么也没做。

我有一个这样的array

//The keys and values are reversed to avoid duplicates in the array keys.
$arr = array(
    'Personal' => array(
                            'PEPE' => "_id",
                            'd' => array(
                                        "full_name" => "Pedro",
                                        "last_name" => "Picapiedras",
                                        "address"=> "La matanza 123",
                                        "department"=> "Soporte"
                                ), //d end
                            'PABLO' => "_id",
                            'd' => array(
                                        "full_name"=> "Pablo",
                                        "last_name"=> "Marmolejo",
                                        "address"=> "Pica  123",
                                        "department"=> "Soporte"
                                ), //d end
        )//personal end
    );//arr end

我想创建_id 字段的索引,以便编辑或查看文档,通过“Pablo”或“Pepe”访问它们。

这可能吗?你能帮我看看我该怎么做吗?

编辑

我试过了

db.reviews.createIndex( { comments: "text" } )  

$user = Array(
        '_id'  => "$id",
        'name' => $name,
    ); 
   $query = Array( '_id' => "$id" );
   $query = Array( '_id' => new MongoId("$id") );

【问题讨论】:

  • 到目前为止你尝试过什么?如果你有代码 sn-ps,它会让人们更容易帮助你。

标签: php mongodb indexing


【解决方案1】:

试试这个:

 $arr = array(
  'PEPE' => array(
    "_id" => 'PEPE',
    'd' => array(
      "full_name" => "Pedro",
      "last_name" => "Picapiedras",
      "address" => "La matanza 123",
      "department" => "Soporte"
    ) //d end
  ), //PEPE END  
  'PABLO' => array(
    "_id" => 'PABLO',
    'd' => array(
      "full_name" => "Pablo",
      "last_name" => "Marmolejo",
      "address" => "Pica  123",
      "department" => "Soporte"
    ), //d end
  ) // Pablo END
); //$arr end


function insertMongo($array) {
  try {
    $mongo = new MongoClient();
    $db = $mongo -> dbName;
    $collection = $db -> collectionName;
    $collection -> insert($array);
  } catch (MongoCursorException $e) {
    die("Query failed ".$e - > getMessage());
  }

} //insertMongo


//To execute:
foreach($arr as $a) {
  insertMongo($a);
};

【讨论】:

  • 可能不是最好的性能,因为大约有 150 条记录要插入然后更新,但工作完美,如果没有其他解决方案......谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-03-20
  • 2016-03-16
  • 1970-01-01
  • 2020-10-08
  • 2012-12-09
  • 1970-01-01
  • 2018-09-27
  • 2014-12-02
相关资源
最近更新 更多