How to enforce foreign keys in NoSql databases (MongoDB)?

后端 未结 6 675
暗喜
暗喜 2020-11-28 15:31

Let\'s say I have a collection of documents such as:

{ \"_id\" : 0 , \"owner\":0 \"name\":\"Doc1\"},{ \"_id\" : 1 , \"owner\":1, \"name\":\"Doc1\"}, etc
         


        
6条回答
  •  清歌不尽
    2020-11-28 16:15

    I would also reccommend that if username's are unique, then use them as the _id. You will save on an index. In the document being stored, set the value of 'owner' in the application as the value of 'username' when the document is created and never let any other piece of code update it.

    If there are requirements to change the owner, then provide appropirate API's with business rules implemented.

    There woudln't be any need of foreign keys.

提交回复
热议问题