Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write empty associative array ({}) to MongoDB from PHP

When I try to insert empty associative array (hashmap/dictionary/dict/{}) to MongoDB from PHP, it is always inserted as non-associative empty array (list/[]). Can I force associative array?

Example:

$m = new Mongo('mongodb://localhost:27017');
$db = $m->selectDB('test');
$collection = $db->selectCollection('test');

// 1: inserts []
$d = array( 'x' => array() );
$collection->insert($d);

// 2: inserts []
$d = array( 'y' => array('a'=>'123') );
unset($d['y']['a']);
$collection->insert($d);

// 3: inserts {}
$d = array( 'z' => array('a'=>'123') );
$collection->insert($d);
$collection->update(array('_id' => $d['_id']), array('$unset' => array('z.a'=>true)));

And the results:

> db.test.find().pretty()
{ "_id" : ObjectId("510fb9ede695c5381a000000"), "x" : [ ] }
{ "_id" : ObjectId("510fb9ede695c5381a000001"), "y" : [ ] }
{ "_id" : ObjectId("510fb9ede695c5381a000002"), "z" : { } }

The third way does what I want, but it is a bit awkward and requires two queries. Is there a better solution?

like image 592
johndodo Avatar asked Dec 26 '22 10:12

johndodo


2 Answers

Whether you store an empty object or array, what's the difference? In BSON, arrays and objects are stored the same way, as a dict. Anyways, to your question.

For the PHP MongoDB driver an empty array is just that, an empty array, so it stores it as an array. When you add a key/value pair to the array, that's how the driver understands it should store an object.

If you really want to store an empty object, try :

$d = new \stdClass();
$collection->insert(array('z' => $d));
like image 68
Adil Avatar answered Jan 04 '23 19:01

Adil


There is in fact a difference in that it will be treated as non-associative [] if empty and will then not allow setting of an associative key, throwing an exception:

MongoCursorException: can't append to array using string field name

You should cast as an object as such:

$d = array( 'x' => (object) array() );
$collection->insert($d);

https://jira.mongodb.org/browse/PHP-172

like image 43
Arran Schlosberg Avatar answered Jan 04 '23 20:01

Arran Schlosberg