duaner1634 2015-08-14 20:53
浏览 280
已采纳

PHP Mongodb创建文本索引

I am new in MongoDB, and I'm trying to create a text index. After trying several hours, I have not accomplished anything.

I have an array like this:

//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

I want to create an Index of the _id field so that to edit or view documents, access them through "Pablo" or "Pepe".

Is this possible? Could you give me a hand on how I can do this?

Edit

I have tried with

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

and

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

1条回答

  • doubi1910 2015-08-15 19:58
    关注

    Try this:

     $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);
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元