Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Lets say I have this document that i'd like to update:

array(
    '_id' => 123,
    'entries' => array(
        '4d8aae834f42e06b638d0000' => array(
            'user_id' => 2,
            'ts' => 'Wed, 23 Mar 2011 19: 37: 55 -0700'
        )
    )
)

I've got an array that i'd like to append into the entries field:

$entries = array(
    '4d8aae834f42e06b638d0000' => array('user_id' => 3, 'ts' => 'Wed, 21 Mar 2011 19: 37: 55 -0700')
    '4d8aae834f42e06b638d3219' => array('user_id' => 4, 'ts' => 'Wed, 22 Mar 2011 19: 37: 55 -0700')
);

The attempted query:

$updateData = array(
    '$pushAll' => array(
        'entries' => $entries
    )
);

$db->update('journal', array('_id' => 123), $updateData, array('upsert' => true));

The above query wouldn't append the new data. Changing the modifier from $pushAll to $set obviously won't work since it'll just replace what's already in there. I've tried $addToSet and it also didn't work. I'm all out of option now.

This is how i would like the document to end up with:

array(
    '_id' => 123,
    'entries' => array(
        '4d8aae834f42e06b638d0000' => array(
            'user_id' => 2,
            'ts' => 'Wed, 23 Mar 2011 19: 37: 55 -0700'
        ),
        '4d8aae834f42e06b638d0000' => array(
            'user_id' => 3, 
            'ts' => 'Wed, 21 Mar 2011 19: 37: 55 -0700'
        ),
        '4d8aae834f42e06b638d3219' => array(
            'user_id' => 4, 
            'ts' => 'Wed, 20 Mar 2011 19: 37: 55 -0700'
        )
    )
)

Thanks in advance.

share|improve this question
1  
The mongo operators like $addToSet and $pushAll will only work with numerically indexed arrays. – Anthony Jack Mar 24 '11 at 13:38

2 Answers

up vote 1 down vote accepted

Just thought I'd give you some info on your working solution. In my experience the reason that you cannot "$push" or "$addToSet" is because your target "entries" is probably an embedded object. You can only use $push, $pushAll, $addToSet on embedded arrays, that is why you had to use the dot notation in order to update as you wanted...

Unfortunately for us php developers a cursor query always returns the data as arrays, the best way to check to see if something like "entries" is an array or object, is to run a find() in the mongo shell and look for the curly/square brackets ( "{}" or "[]" ) in the result.

share|improve this answer

After more testing; ive managed to find a working solution if anyone is interested.

Setting the keys in your new data with the prefix 'entries' (or whatever field name you're updating) with the modifier to '$set' will do the trick.

$entries = array(
    'entries.4d8aae834f42e06b638d0000' => array('user_id' => 3, 'ts' => 'Wed, 21 Mar 2011 19: 37: 55 -0700')
    'entries.4d8aae834f42e06b638d3219' => array('user_id' => 4, 'ts' => 'Wed, 22 Mar 2011 19: 37: 55 -0700')
);

And for the update:

$updateData = array(
    '$set' => $entries
);

$db->update('journal', array('_id' => 123), $updateData, array('upsert' => true));
share|improve this answer

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.