[英]How to query relations in NoSQL?
I have decided to move my database from MySQL to Mongo because majority of the time, my data is not structured. 我决定将我的数据库从MySQL移到Mongo,因为大多数时候,我的数据不是结构化的。 And it allowed me possibilities that was too complex in a traditional SQL.
它让我在传统的SQL中过于复杂。
There is one problem that I am currently facing and how to approach a traditional relational model of SQL in NoSQL. 我目前面临的一个问题是如何在NoSQL中使用传统的SQL关系模型。 I have read many times that NoSQL is not designed to deal with relations.
我已多次读过NoSQL不是为处理关系而设计的。 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.
在SQL中,我有一个单独的oauth访问令牌表,其中包含user_id,client_id,access_token,expires作为其属性。 It was 1-N relation between a user and an access_token.
用户和access_token之间是1-N关系。 How would I do that in NoSQL?
我如何在NoSQL中做到这一点? By adding an array field oauth_tokens?
通过添加数组字段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"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.