$result
=
$collection
->insert(
$array
); #简单插入
echo
“新记录 ID:”.
$array
['_id']; #MongoDB 会返回一个记录标识
var_dump(
$result
); #返回:bool(true)
//**向集合中安全插入数据,返回插入状态(数组). **/
$array
=
array
(‘column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai2′);
$result
=
$collection
->insert(
$array
,true); #用于等待 MongoDB 完成操作,以便确定是否成功.
(当有大量记录插入时使用该参数会比较有用)
echo
“新记录 ID:”.
$array
['_id']; #MongoDB 会返回一个记录标识
var_dump(
$result
); #返回:
array
(3) { ["err"]=> NULL ["n"]=> int(0) ["ok"]=> float(1) }
//**
完整的写法 **/
#insert(
$array
,
array
(‘safe'=>false,'fsync'=>false,'timeout'=>10000))
/*
* *
* 完整格式:insert ( array $a [, array $options = array() ] )
* insert(array(),array(‘safe'=>false,'fsync'=>false,'timeout'=>10000))
* 参数:safe:默认 false,是否安全写入
* fsync:默认 false,是否强制插入到同步到磁盘
* timeout:超时时间(毫秒)
*
* 插入结果:{ “_id” : ObjectId(“4d63552ad549a02c01000009″), “column_name” : “col770″,
“column_exp” : “xiaocai” }
* '_id'为主键字段,在插入是 MongoDB 自动添加.
*
* 注意:1.以下两次插入的为同一条记录(相同的_id),因为它们的值相同。
* $collection->insert(array(‘column_name'=>'xiaocai'));
* $collection->insert(array(‘column_name'=>'xiaocai'));
* 避免
* $collection->insert(array(‘column_name'=>'xiaocai'),true);
* try {
* $collection->insert(array(‘column_name'=>'xiaocai'),true);
* }catch(MongoCursorException $e){
* echo “Can't save the same person twice!\n”;
* }
*
* 详细资料:
http://www.php.net/manual/zh/mongocollection.insert.php
* *
*/
//*************************
//**
更新文档 **//
//*************************
//**
修改更新 **/
$where
=
array
(‘column_name'=>'col123′);
$newdata
=
array
(‘column_exp'=>'GGGGGGG','column_fid'=>444);
$result
=
$collection
->update(
$where
,
array
(‘
$set
'=>
$newdata
)); #
$set
:让某节点等于给定值,类似