PHP Mongodb创建文本索引


PHP Mongodb create Text Index

我是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") );

试试这个:

 $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);
};