How to update a whole document in MongoDB using PHP?

119 views
Skip to first unread message

Ausaf Sharif (C.R)

unread,
Aug 19, 2015, 5:21:25 PM8/19/15
to mongodb-user
I want to update my older entries through PHP interface in MongoDB. First I get data from the text fields and then store that into variables and then using those variables to update data in Mongodb here is my code please help me i tried all ways but every time disappointment.
<?php
                   
if(isset($_REQUEST['btn']))
                   
{
                        $a
=$_REQUEST['textfield'];
                        $b
=$_REQUEST['textfield2'];
                        $c
=$_REQUEST['textfield3'];
                        $d
=$_REQUEST['textfield4'];
                        $e
=$_REQUEST['textfield5'];
                        $f
=$_REQUEST['textfield6'];
                        $g
=$_REQUEST['textfield7'];
                        $h
=$_REQUEST['textfield8'];

                         $m
= new MongoClient();         // connect to mongodb
                         $db
= $m->app;             // select a database named app                          
                         $doct
= array('$set' => array( "Encoding" => $b,
                                                       
"Pos" => $c,
                                                       
"Roman" => $d,
                                                       
"Important" => $e,
                                                       
"Hindi" => $f,
                                                       
"English" => $g,
                                                       
"Type" => $h,
                                                       
)
                                       
);
                        $db
->lafaz->update(array("_id"=> new MongoID($a)),$doct, array('multiple' => true));                
                        header
('Location:page.php');
                   
}
                     
?>


Jeremy Mikola

unread,
Aug 19, 2015, 5:52:49 PM8/19/15
to mongod...@googlegroups.com
To start, it'd be helpful to share the return value of MongoCollection::update(). That should at least tell you if any documents are being matched by the criteria and, if any are matched, how many are being modified.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/1ab59995-3e6b-41cf-b644-527183f81662%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ausaf Sharif (C.R)

unread,
Aug 20, 2015, 1:31:17 AM8/20/15
to mongodb-user
@JeremyMikola I didn't get what are you saying are you saying about getting data from text fields or what???

Jeremy Mikola

unread,
Aug 20, 2015, 1:37:39 AM8/20/15
to mongod...@googlegroups.com
I assumed from your original post that you were having trouble executing the update query, or that it was not resulting in a matching document being updated at all. My response was suggesting that you dump the return value from $db->lafaz->update(...), which is a call to MongoCollection::update(). That return value should report how many documents were matched and modified by the update.

Additionally, I'll point out that setting the "multiple" option to true does not make much sense if you are specifying an "_id" field as your update criteria. Each document in the collection must have a unique "_id" value, so that criteria could only match at most one document.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

krazzyj...@gmail.com

unread,
Aug 21, 2015, 5:30:47 AM8/21/15
to mongodb-user
these are my files i attach here now you suggest me more perfectly..


On Thursday, August 20, 2015 at 2:21:25 AM UTC+5, Ausaf Sharif (C.R) wrote:
edit.php
form.css

Jeremy Mikola

unread,
Aug 22, 2015, 1:08:11 AM8/22/15
to mongod...@googlegroups.com
On Fri, Aug 21, 2015 at 5:13 AM, <krazzyj...@gmail.com> wrote:
these are my files i attach here now you suggest me more perfectly..

Those attachments don't tell us much more than the original code you shared in the first post.

Are you not able to var_dump() the return value from the update() method?

Alternatively, is this code generating any exceptions or errors when executing?


krazzyj...@gmail.com

unread,
Aug 22, 2015, 2:14:44 AM8/22/15
to mongodb-user
no i m not able to var_dump() and yes there are two errors both have something related to mongoID().


On Thursday, August 20, 2015 at 2:21:25 AM UTC+5, Ausaf Sharif (C.R) wrote:

Jeremy Mikola

unread,
Aug 22, 2015, 2:23:50 AM8/22/15
to mongod...@googlegroups.com
On Sat, Aug 22, 2015 at 2:14 AM, <krazzyj...@gmail.com> wrote:
no i m not able to var_dump() and yes there are two errors both have something related to mongoID().

Why did you not share those errors in the first post? Surely that is relevant to the script not functioning properly.

Since you're only constructing a MongoId object in your code, I assume an exception is being thrown because the value is not a 24-character hexadecimal string. This is discussed in the class documentation.

Internally the MongoId (and BSON ObjectId) is a 12-byte value. When casting a MongoId to a string (e.g. to print, store in a text field, or pass in a URL) we convert it to a 24-character hexadecimal string, which is also the same format expected in the constructor. Alternatively, if you do not pass the constructor a value, we generate the 12-byte value (as discussed in the ObjectId documentation).

krazzyj...@gmail.com

unread,
Aug 24, 2015, 6:18:24 AM8/24/15
to mongodb-user
now please tell me what change i make in the code to run it properly.


On Thursday, August 20, 2015 at 2:21:25 AM UTC+5, Ausaf Sharif (C.R) wrote:

Jeremy Mikola

unread,
Aug 25, 2015, 3:23:36 PM8/25/15
to mongod...@googlegroups.com
On Mon, Aug 24, 2015 at 6:18 AM, <krazzyj...@gmail.com> wrote:
now please tell me what change i make in the code to run it properly.

I don't have any additional suggestion beyond ensuring that the ID value submitted through the form (element name "textfield" in your code) is a 24-character hexadecimal string.

Since you're echoing $obj["_id"] directly when generating the form, one way this might fail is if your document was using something other than an ObjectId (i.e. MongoId in the PHP driver) for its "_id" field. If the "_id" was an embedded document, you could get an error attempting to convert an array to a string via the echo statement. Alternatively, printing an integer ID would be just fine, but you'd get an exception constructing a MongoId from it after submitting the form.

One way to make the code more defense here would be to check that $obj["_id"] is in instance of MongoId during each loop iteration and throwing an exception (or skipping the iteration) if that's not the case.

Reply all
Reply to author
Forward
0 new messages