converting mongo id to string in php

3,056 views
Skip to first unread message

kehers

unread,
Nov 1, 2010, 1:54:15 AM11/1/10
to mongodb-user
im having issues converting the mongo id (_id) returned as an object
to string in php.
While ths works perfectly on my local server:
$cursor = $db->tbl->findOne();
echo $cursor['_id'].'';
//or (string) $cursor['_id']
it doesnt on my online server. These
var_dump($cursor['_id'].'');
will always return something like
string "object #14" instead of the hexadecimal representatn of the id
my online server mongodb version is 1.6.2

Kristina Chodorow

unread,
Nov 1, 2010, 11:10:11 AM11/1/10
to mongod...@googlegroups.com
var_dump doesn't convert an object to a string, it dumps the type (object) and any fields (MongoId doesn't have any fields).  Try doing echo $cursor['_id'].'' or (string) $cursor['_id'].



--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


kehers

unread,
Nov 1, 2010, 12:44:34 PM11/1/10
to mongodb-user
Sure did. Still got the same thing :(

Kristina Chodorow

unread,
Nov 1, 2010, 12:55:46 PM11/1/10
to mongod...@googlegroups.com
Is it using PHP 5.1?  If so, it won't do implicit string casting, you have to call $cursor['_id']->__toString().  If not, what is the code you're running?


On Mon, Nov 1, 2010 at 12:44 PM, kehers <keh...@gmail.com> wrote:
Sure did. Still got the same thing :(

--
Reply all
Reply to author
Forward
0 new messages