I have decided to move my database from MySQL to Mongo because majority of the time, my data is not structured. And it allowed me possibilities that was too complex in a traditional SQL.
There is one problem that I am currently facing and how to approach a traditional relational model of SQL in NoSQL. I have read many times that NoSQL is not designed to deal with relations. Do I need to add them as an array to the document with a relation?
Here is one situation that has made me stuck. In SQL I had a separate table for oauth access tokens that has user_id, client_id, access_token, expires as its attributes. It was 1-N relation between a user and an access_token. How would I do that in NoSQL? By adding an array field oauth_tokens? If I do that, how do I search for the token in the array? How do I query
search for a document where the _id is $user_id and there is an element
with $token in the access_tokens array?
You have at least 2 options here:
Example:
db.tokens.insert({ client_id : "1", user_id : "20", access_token : "1234567890", expires : new Date(2014-12-31)})
query:
db.tokens.find({user_id:"20"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With