使用mongo与RDBMS比较时如何设计与id相关的应用程序url?
How to design application url related with id when use mongo comparison with RDBMS?
假设有一个显示一些视频的网络应用程序。我认为在设计 url(不是 API)时很常见。
http://myservice.com/videos/{video-id}
使用 RDBMS 实现时策略很好,因为 ID 通常很短,但是,我一直在努力解决使用 MongoDB 的问题。
MySQL 案例:
http://myservice.com/videos/1
Mongo 案例:
http://myservice.com/videos/57932d53a1b33ab926ab54b3
恕我直言,表示 url 太长了。我不想看到像下面这样的东西
http://myservice.com/videos/57932d53a1b33ab926ab54b3/comments/57932d53a1b33ab926ab54c2
我是否必须更改表示视频名称或排序顺序编号的方式?有没有更好的方法来表示标识符?
您作为示例提供的 ID 看起来像是自动生成的 ObjectId 值。您可以使用任何值来表示 _id 字段,只要它们在单个集合中是唯一的即可。
The field name _id is reserved for use as a primary key; its value
must be unique in the collection, is immutable, and may be of any type
other than an array. (See the documentation)
另一种方法是在您的文档中添加一个可读格式的字段,用于 url 形成目的(并在需要时为其创建一个唯一索引)。
假设有一个显示一些视频的网络应用程序。我认为在设计 url(不是 API)时很常见。
http://myservice.com/videos/{video-id}
使用 RDBMS 实现时策略很好,因为 ID 通常很短,但是,我一直在努力解决使用 MongoDB 的问题。
MySQL 案例:
http://myservice.com/videos/1
Mongo 案例:
http://myservice.com/videos/57932d53a1b33ab926ab54b3
恕我直言,表示 url 太长了。我不想看到像下面这样的东西
http://myservice.com/videos/57932d53a1b33ab926ab54b3/comments/57932d53a1b33ab926ab54c2
我是否必须更改表示视频名称或排序顺序编号的方式?有没有更好的方法来表示标识符?
您作为示例提供的 ID 看起来像是自动生成的 ObjectId 值。您可以使用任何值来表示 _id 字段,只要它们在单个集合中是唯一的即可。
The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array. (See the documentation)
另一种方法是在您的文档中添加一个可读格式的字段,用于 url 形成目的(并在需要时为其创建一个唯一索引)。