note 105485 added to mongocollection.update
| From: | nerds at limeworks dot com dot au | Date: | Mon, 22 Aug 2011 09:39:08 +0000 |
| Subject: | note 105485 added to mongocollection.update | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182632@lists.php.net to get a copy of this message | ||
For anyone referencing records by the Mongo _id object, it's important to recognise that it is
in fact an object, and not a string.
If you have a record with a Mongo ID of say "4e519d5118617e88f27ea8cd" that you are trying
to retrieve or update, you cannot search for it using something like:
<?php
$m = new Mongo();
$db = $m->selectDB('db');
$collection = 'collection';
$db->$collection->findOne(array('_id', '4e519d5118617e88f27ea8cd'));
?>
There is some documentation that mentions simple conversion to string will solve this, but I have
found the only reliable way to locate records based on their ID is to first pass it to MondoID(),
then use that for reference.
Something like this will be far more reliable:
<?php
$m = new Mongo();
$db = $m->selectDB('db');
$collection = 'collection';
$mongoID = new MongoID('4e519d5118617e88f27ea8cd');
$db->$collection->findOne(array('_id', $mongoID));
?>
This may prove useful for anyone using the ID object like an auto-increment database key would be
used in MySQL or similar.
----
Server IP: 69.147.83.197
Probable Submitter: 203.45.149.26
----
Manual Page -- http://www.php.net/manual/en/mongocollection.update.php
Edit -- https://master.php.net/note/edit/105485
Del: integrated -- https://master.php.net/note/delete/105485/integrated
Del: useless -- https://master.php.net/note/delete/105485/useless
Del: bad code -- https://master.php.net/note/delete/105485/bad+code
Del: spam -- https://master.php.net/note/delete/105485/spam
Del: non-english -- https://master.php.net/note/delete/105485/non-english
Del: in docs -- https://master.php.net/note/delete/105485/in+docs
Del: other reasons-- https://master.php.net/note/delete/105485
Reject -- https://master.php.net/note/reject/105485
Search -- https://master.php.net/manage/user-notes.php