Skip to content

Instantly share code, notes, and snippets.

@catchamonkey
Created January 21, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catchamonkey/e26eb7607f7a2893af5a to your computer and use it in GitHub Desktop.
Save catchamonkey/e26eb7607f7a2893af5a to your computer and use it in GitHub Desktop.

Given the DB is in the state at the end of the post: http://jwage.com/post/30490176882/multiple-levels-of-embedded-documents-in-mongodb and we have, in Mongo the structure of

Array
(
    [_id] => MongoId Object
        (
        )
    [username] => jwage
    [profiles] => Array
        (
            [0] => Array
                (
                    [name] => Profile #1
                    [addresses] => Array
                        (
                            [0] => Array
                                (
                                    [number] => 6512
                                    [street] => Mercomatic
                                    [city] => Nashville
                                    [state] => Tennessee
                                    [zipcode] => 37209
                                )
                        )
                )
            [1] => Array
                (
                    [name] => Profile #2
                    [addresses] => Array
                        (
                            [0] => Array
                                (
                                    [number] => 475
                                    [street] => Buckhead Ave
                                    [city] => Atlanta
                                    [state] => Georgia
                                    [zipcode] => 30303
                                )
                        )
                )
        )
)

How do you go about adding a new Address to one Profile of many of a specific User?

Create the new Address.

$newAddress = new Address();
$newAddress->setCity('Albany'); // etc

Attach to the profile within the 'top level' User document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment