1

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;
1
  • I am using laravel 5.4 so for that i write databse code in model and call it in controller and display the output. so now I want to write same code for updating,inserting and deleting the data. please any suggestion how can i do it.. Commented Feb 22, 2017 at 6:57

1 Answer 1

1

Insert:

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

Update:

$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>';
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have to write a code in model. so for that any suggestion?
I am using laravel so for that I write databse code in model then call it controller
Get the basic idea of model in that framework and try it yourself
I have done it for retrieval but now I want to know how can i write the code for update, insert and delete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.