Oleg
Oleg

Reputation: 2821

Using numeric field names with the MongoDB PHP Driver

Could you please tell me if it's possible to use numberic field names in mongodb, something like this: {"1" : 'value1', "2" : 'value2', "3" : 55}. It looks like I can input such data using mongodb command line, but I have problems when I try to write such data using php, getting Message: field names must be strings error.

I found about naming of collections in mongodb here http://www.mongodb.org/display/DOCS/Collections, but I didn't find info about naming of fields names. Thank you.

I tried this one for arrays in php:

$elements[1] = 1;
$index = "2";
settype($index, "string");
$elements[$index] = 2;
$elements["3"] = 3;
var_dump($elements);

And it displays:

array
  1 => int 1
  2 => int 2
  3 => int 3

The error I talk about is:

An error occurred Application error Exception information:

Message: field names must be strings Stack trace:

#0 C:\library\Shanty\Mongo\Collection.php(376): MongoCollection->find(Array, Array)
#1 C:\git_reps\mailable\application\models\Subscriber1.php(191): Shanty_Mongo_Collection::all(Array, Array)
#2 C:\git_reps\mailable\application\models\Subscriber1.php(203): Model_Subscriber1::getCursor(Array, Array, Array)
#3 C:\git_reps\mailable\application\controllers\ListsController.php(639): Model_Subscriber1::getPaginator(Array, Array, Array)
#4 C:\library\Zend\Controller\Action.php(513): ListsController->view1Action()
#5 C:\library\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('view1Action')
#6 C:\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#7 C:\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#8 C:\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#9 C:\git_reps\mailable\public\index.php(25): Zend_Application->run()
#10 {main}  

Request Parameters:

array (
  'controller' => 'lists',
  'action' => 'view1',
  'module' => 'default',
  'id' => '52',
)  

It happens when I try to get mongodb cursuro setting fields something like "1".

Upvotes: 2

Views: 1123

Answers (3)

kris
kris

Reputation: 23592

The PHP driver is being a bit overzealous about guarding you from returning fields that are numbers. Luckily, you can hack around it.

So, this doesn't work, because the MongoCursor constructor checks for it:

$cursor = $collection->find($criteria, array("2" => 1));

But this does, because the fields method doesn't have the same checks as the constructor:

$cursor = $collection->find($criteria)->fields(array("2" => 1));

I've filed a bug for this: https://jira.mongodb.org/browse/PHP-338

(Btw, in the future it helps us debug when give a bigger code sample of what you're doing.)

Upvotes: 3

Adam Comerford
Adam Comerford

Reputation: 21682

My guess is that PHP is doing some sort of auto conversion when you are using the number as a string. Try using settype (http://php.net/manual/en/function.settype.php) to make sure it is getting set as a string and not converted to an integer, see if that allows you to have the same behavior as the MongoDB shell.

Upvotes: 4

rompetroll
rompetroll

Reputation: 4799

As you store json documents in mongodb, you should refer to http://www.json.org/ for naming specifications. The definition of an object there says that keys must be strings.

Therefore you must wrap the number as a String, then you can use it as attribute name.

Upvotes: 3

Related Questions