我决定将我的数据库从MySQL移到Mongo,因为大多数时候,我的数据不是结构化的.它让我在传统的SQL中过于复杂.
我目前面临的一个问题是如何在NoSQL中使用传统的SQL关系模型.我已多次读过NoSQL不是为处理关系而设计的.我是否需要将它们作为数组添加到具有关系的文档中?
这种情况让我陷入困境.在SQL中,我有一个单独的oauth访问令牌表,其中包含user_id,client_id,access_token,expires作为其属性.用户和access_token之间是1-N关系.我如何在NoSQL中做到这一点?通过添加数组字段oauth_tokens?如果我这样做,我如何在数组中搜索令牌?我该如何查询
search for a document where the _id is $user_id and there is an element
with $token in the access_tokens array?
Run Code Online (Sandbox Code Playgroud)
您在这里至少有 2 个选择:
例子:
db.tokens.insert({ client_id : "1", user_id : "20", access_token : "1234567890", expires : new Date(2014-12-31)})
Run Code Online (Sandbox Code Playgroud)
询问:
db.tokens.find({user_id:"20"})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8219 次 |
| 最近记录: |