Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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".

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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.)

share|improve this answer
    
I try to use Shanty library working with Zend Framework. It throws exception in Collection.php using on line: $cursor = static::getMongoCollection(false)->find($query, $fields); in method that tries to read data from mongodb. –  Oleg Feb 29 '12 at 4:59
    
I replaced: $cursor = static::getMongoCollection(false)->find($query, $fields); with this one in Collection.php of Shanty library: $cursor = static::getMongoCollection(false)->find($query)->fields($fields); And it looks like it works with fields names consisting of number only, like "1". –  Oleg Feb 29 '12 at 5:33
    
Thank you very much, Kristina. It seems it solves the problem. Maybe this bug will be fixed in php driver later. –  Oleg Feb 29 '12 at 5:36

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.

share|improve this answer
    
Can I use field something like "2" in php and mongodb somehow? For example, I can save document in mongodb console {"2" : "test"} But I get any exception in php when I try to read it from mongodb. –  Oleg Feb 28 '12 at 14:51
    
Have a look at Adam C's answer, he might be onto something there. –  rompetroll Feb 28 '12 at 15:18

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.

share|improve this answer
1  
That's why I don't like PHP. It tries to be smarter than me :) –  Sergio Tulentsev Feb 28 '12 at 15:06
    
It's rather strange to me that it converts "1" to 1 index. Frankly speaking I haven't faced it and thought about it. –  Oleg Feb 28 '12 at 15:14
    
I edited my post displaying converting number index to string, but it looks like it doesn't work. –  Oleg Feb 28 '12 at 15:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.