MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

cara memperbarui dan memasukkan catatan di php menggunakan mongodb

Sisipkan:

$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'  => '[email protected]', '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>';
}

Pembaruan:

$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>';
}



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Cara mengonfigurasi sumber data StrongLoop LoopBack MongoDB untuk diterapkan ke Heroku

  2. Tidak ada deserializer Json yang ditemukan untuk tipe Option[reactivemongo.bson.BSONObjectID]

  3. SSL luwak, koneksi tidak diterima

  4. Luwak async/tunggu temukan lalu edit dan simpan?

  5. alternatif mongoDB untuk batasan kunci asing