2
$update = $col->update(array('_id' => 'test'), array('$push' => array('friends' => 1)), array('safe' => true, 'upsert' => true));
$test = $col->findOne(array('_id' => 'test'));
debug(compact('update', 'test'));

Does not work as expected.

I get (checking via RockMongo) :

{
   "_id": "test",
   "friends": {
     "0": 1 
   } 
}

I want/expect to have this :

{
   "_id": "test",
   "friends": [
     1 
   ] 
}

Any ideas ? Thanks !

1
  • How about array('friends' => array(1))? Commented May 26, 2011 at 15:22

1 Answer 1

6

I spent a good amount of time a while back troubleshooting why my data was getting inserted wrongly only to find out that RockMongo doesn't show the data correctly.

It's belive its because RockMongo isn't really showing the data as a Mongo document, but instead as PHP.

I found this to differ for a couple of different datatypes between firefox and chrome for some reason ... arrays, dates, MongoID's

But in the end, I did a find() on the command line and it was correct.

I wouldn't trust what RockMongo is telling you, see an example below.

Also is there a reason you need to use "safe" ?

Actual Data

enter image description here

What RockMongo says (wrong)

enter image description here

2
  • Should have thought of that! Just used safe for debug purposes (waiting for return, etc). Thanks. Commented May 26, 2011 at 20:27
  • Also @olouv, you want to be aware of some craziness with PHP and arrays vs JSON objects. I have an example here from a presentation I did, github.com/gatesvp/php_get_started/blob/master/step10.php Commented May 27, 2011 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.