2015-08-14 83 views
1

我在MongoDB中是新的,而我試圖創建一個文本索引。 嘗試幾個小時後,我沒有做任何事情。PHP 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領域的一個索引,使編輯或查看文檔,通過「帕布洛」或「佩佩」訪問它們。

這可能嗎?你能幫我一下我怎麼能做到這一點?

編輯

我與

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

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

你嘗試過這麼遠嗎?如果你有代碼片段,它可以讓人們更容易地幫助你。 – khuderm

回答

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); 
}; 
+0

也許不是最好的表現,因爲有大約150條記錄插入,然後更新,但工作完美,如果沒有其他解決方案... 謝謝! – Javi