How to write empty associative array ({}) to MongoDB from PHP
associative-array, mongodb, php
Solution
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));
Problem
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?