How do you track record relations in NoSQL?

后端 未结 5 2029
鱼传尺愫
鱼传尺愫 2020-12-04 05:28

I am trying to figure out the equivalent of foreign keys and indexes in NoSQL KVP or Document databases. Since there are no pivotal tables (to add keys marking a relation be

5条回答
  •  [愿得一人]
    2020-12-04 06:07

    You have

    "user": {
        "userid": "unique value",
        "category": "student",
        "metainfo": "yada yada yada",
        "clubs": ["archery", "kendo"]
    }
    
    "comments": {
        "commentid": "unique value",
        "pageid": "unique value",
        "post-time": "ISO Date",
        "userid": "OP id -> THIS IS IMPORTANT"
    }
    
    "page": {
        "pageid": "unique value",
        "post-time": "ISO Date",
        "op-id": "user id",
        "tag": ["abc", "zxcv", "qwer"]
    }
    

    Well in a relational database the normal thing to do would be in a one-to-many relation is to normalize the data. That is the same thing you would do in a NoSQL database as well. Simply index the fields which you will be fetching the information with.

    For example, the important indexes for you are

    • Comment.UserID
    • Comment.PageID
    • Comment.PostTime
    • Page.Tag[]

    If you are using NosDB (A .NET based NoSQL Database with SQL support) your queries will be like

     SELECT * FROM Comments WHERE userid = ‘That user’;
    
     SELECT * FROM Comments WHERE pageid = ‘That user’;
    
     SELECT * FROM Comments WHERE post-time > DateTime('2016, 1, 1');
    
     SELECT * FROM Page WHERE tag = 'kendo'
    

    Check all the supported query types from their SQL cheat sheet or documentation.

提交回复
热议问题