I am new to dynamodb but worked with MongoDB relationship. I am very confused while reading aws docs for dynamodb one to many relationship.
So my scenario is:
For example, I have tables user and addresses:
Table: User
----
username
fname
lname
address: ref to addresses
Table: Addresses
----------
address line 1
address line 2
city
Zip
state
country
Can you please help me how to achieve this in dynamodb.
DynamoDB is not a relational database. You cannot perform the type of relational requests that you are thinking about (particularly in a single query as described in your question #2). It requires a different way of thinking about how you will store and access your data, and accordingly it may not be the appropriate technology for your application.
Dynamo does not offer foreign keys or table joins. Each table has either a Partition Key (PK), or a combination of a Partition Key and Sort Key (SK). When a table has just a PK, it can only be queried for individual items. When it has both a PK and an SK, it can be queried for a single item by PK/SK, or it can be queried for all items that share the same PK.
One way of implementing 1-to-N data as you describe would look something like this:
Table: Users
------------
[PK] userId
username
fname
lname
Table: Addresses
----------------
[PK] userId
[SK] addressId
address line 1
address line 2
city
zip
state
country
In this layout, you would still require two calls to Dynamo to get all information for a user, including addresses. You would make a single getItem call to the Users table by that userId, and you would make a query call to the Addresses by that same userId. The first call would return the single user record, and the second call would return a list of address records, sorted by addressId.
You must manually manage your data. As there are no foreign keys, there are also no constraints or cascades. It's very easy to end up with orphaned data if you are not careful. Again, Dynamo may not be the most appropriate tool for your application.
What I've described here only scratches the surface. For anyone hoping to build enterprise software using DynamoDB for data storage, it is paramount that you have a good understanding of its architectural approach and best practices. Coming from a SQL or even MongoDB background, your intuition may lead you astray, causing you to build something that will not perform well and will be hard to debug. It is an extremely powerful tool for the right types of jobs, but you must perform due diligence to understand it first.
--
Edited to add: the approach I described above is not even a good one! AWS has shared documentation and talks about this sort of thing, including their Best Practices for DynamoDB which goes well beyond the scope of this answer.