2

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.

1
  • 1
    The mongo operators like $addToSet and $pushAll will only work with numerically indexed arrays. Commented Mar 24, 2011 at 13:38

2 Answers 2

4

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));
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

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.