如何使用mongodb在php中更新和插入记录 [英] how to update and insert records in php using mongodb
本文介绍了如何使用mongodb在php中更新和插入记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用php和数据库更新和插入数据将是mongodb
How to update and insert the data using php and database will be mongodb
'[
{
_id: 5,
unique: true,
name: "Joseph",
password: "mangalore"
},
{
_id: 9,
unique: true,
name: "John",
password: {
passcode1: 1,
passcode2: 2
}
},
{
_id: 10,
unique: true,
name: "Ashraf",
password: {
passcode1: 2,
passcode2: 3
}
},
{
_id: 11,
unique: true,
name: "Rajesh",
password: {
passcode1: 3,
passcode2: 4
}
}
]`
请告诉我如何更新和插入数据. 我已经完成了检索并且代码是
Please tel me how to update and insert the data. i have already done for retrieving and code is
$id = DB::connection('mongodb')->collection('login2')->where('password.passcode1','>',1)->get();
return $id;
推荐答案
插入:
$mongo = new MongoClient();
$db = $mongo->mydb1;
/* Note: In mongodb if the specified collection is not present than it will automatically create it and the document is inserted in the newly created collection */
$data = array('emp_id' => '1', 'first_name' => 'Tiger' , 'last_name' => 'Nixon', 'position' => 'System Architect', 'email' => 't.nixon@datatables.net', 'office' => 'Edinburgh', 'start_date' => '2011-04-25 00:00:00', 'age' => '61', 'salary' => '320800', 'projects' => array('Project1', 'Project2', 'Project3'));
$collection = $db->createCollection("emp_details");
if($collection->insert($data))
{
echo '<p style="color:green;">Record inserted successfully</p>';
}
else
{
echo '<p style="color:red;">Error in insertion</p>';
}
更新:
$mongo = new MongoClient();
$db = $mongo->mydb1;
/* Note: Here we are using the update() method. The update() method update values in the existing document */
$collection = $db->createCollection("emp_details");
$newdata = array('$set' => array("age" => "55", "salary" => "320000"));
// specify the column name whose value is to be updated. If no such column than a new column is created with the same name.
$condition = array("emp_id" => "1");
// specify the condition with column name. If no such column exist than no record will update
if($collection->update($condition, $newdata))
{
echo '<p style="color:green;">Record updated successfully</p>';
}
else
{
echo '<p style="color:red;">Error in update</p>';
}
这篇关于如何使用mongodb在php中更新和插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文