我是MongoDB的新手,并在Windows上使用它。我创建了一个数据库,然后在那里创建了一个Collection并插入了记录,我正在通过GUI工具RoboMongo进行检查,它在GUI工具上显示,但是当我通过代码获取它时,它什么也没显示。 以下是代码,请指导我做什么。
$m = new MongoClient('mongodb://root:root123@localhost');
$db = $m->selectDB('db_name');
$collection_name = 'collection_name';
$db->createCollection( $collection_name );
$get_collection = new MongoCollection( $db, $collection_name );
$data = array(
'_id' => '1_record',
'name' => 'Majid Ali',
'designation' => 'Developer'
);
$get_collection->insert( $data );
$find = $get_collection->find();
print_r( $find );
答案 0 :(得分:0)
try{
// Connecting to server
$m = new MongoClient('mongodb://root:root123@localhost');
}catch(MongoConnectionException $connectionException){
print $connectionException;
exit;
}
try{
// Getting MongoCollection
$db = $m->db_name;
}catch(MongoException $mongoException){
print $mongoException;
exit;
}
$collection_name = 'collection_name';
$db->createCollection( $collection_name );
//Selected the Collection
$get_collection= $db->collection_name;
$data = array(
'_id' => '1_record',
'name' => 'Majid Ali',
'designation' => 'Developer'
);
try{
// Inserting data into students collection
$get_collection->insert( $data );
}catch(MongoCursorException $mongoCursorException){
echo $mongoCursorException;
exit;
}
$find = $get_collection->find();
$array = iterator_to_array($find );
print_r( $array );