PHP Mongodb创建文本索引

我是MongoDB的新手,我正在尝试创建一个文本索引。
尝试几个小时后,我 没有完成任何事情。</ p>

我有一个 array </ code>,如下所示:</ p>

  //键和值 反转以避免数组键中的重复。
$ arr = array(
'个人'=&gt;数组(
'PEPE'=&gt;“_id”,
'd'=&gt;数组(\ n“full_name”=&gt;“Pedro”,
“last_name”=&gt;“Picapiedras”,
“地址”=&gt;“La matanza 123”,
“department”=&gt;“Soporte”
),// d结束
'PABLO'=&gt;“_ id”,
'd'=&gt;数组(
“full_name”=&gt;“Pablo”,
“last_name”=& GT; “Marmolejo”,
“地址”=&gt; “Pica 123”,
“department”=&gt; “Soporte”
),// d结束
)//个人结束
); // arr结束
</ code> </ pre>

我想创建一个索引 _id </ code>字段,以便编辑或查看文档,通过“Pablo”或“Pepe”访问它们。</ p>

这可能吗? 您能否告诉我如何做到这一点?</ p>

编辑</ strong> </ p>

我试过</ p >

  db.reviews.createIndex({comments:“text”})
</ code> </ pre>

和</ p>
\ n

  $ user = Array(
'_id'=&gt;“$ id”,
'name'=&gt; $ name,
);

$ query = Array('_ id'=&gt;“$ id”);
$ query = Array('_ id'=&gt; new MongoId(“$ id”));
</ code> </ pre>
</ div>

展开原文

原文

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

douzhongju8780
douzhongju8780 toolong
大约 5 年之前 回复

1个回答



试试这个:</ p>

  $ arr = array(
'PEPE'= &gt; array(
“_id”=&gt;'PEPE',
'd'=&gt;数组(
“full_name”=&gt;“Pedro”,
“last_name”=&gt;“Picapiedras”,

“地址”=&gt;“La matanza 123”,
“department”=&gt;“Soporte”
)// d结束
),// PEPE END
'PABLO'=&gt;数组(

“_id”=&gt;'PABLO',
'd'=&gt;数组(
“full_name”=&gt;“Pablo”,
“last_name”=&gt;“Marmolejo”,
“地址 “=&gt;”Pica 123“,
”department“=&gt;”Soporte“
),// d end
)// Pablo END
); // $ arr end

function insertMongo($ array){
try {
$ mongo = new MongoClient();
$ db = $ mongo - &gt; dbName;
$ collection = $ db - &gt; collectionName;
$ collection - &gt; insert($ array);
} catch(MongoCursorException $ e){
die(“查询失败”。$ e - &gt; getMessage());
}

} // insertMongo

//执行:
foreach($ arr as $ a){
insertMongo($ a);
};
</ code> </ pre>
</ div>

展开原文

原文

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

douchun5976
douchun5976 也许不是最好的表现,因为有大约150条记录要插入然后更新,但工作得很好,如果没有其他解决方案......谢谢!
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐