简体   繁体   English

向我的网站添加存档功能

[英]Adding an archiving feature to my website

I'm trying to add post archiving to my website and give users the opportunity to archive any post they need.我正在尝试将帖子存档添加到我的网站,并让用户有机会存档他们需要的任何帖子。 My website is built with MERN and a RESTful api.我的网站是用 MERN 和 RESTful api 构建的。

  1. My first thought was to add a section in my Post model like this:我的第一个想法是在我的帖子 model 中添加一个部分,如下所示:

{ archived: { type: Boolean, default: false } } {归档:{类型:Boolean,默认:假}}

Where users select "archive" on the front end and a request is sent to the server which switches that model instance/post to archived: true.用户 select 在前端“归档”,请求被发送到服务器,服务器将 model 实例/帖子切换到归档:true。 Then, the all posts page should only display unarchived posts.然后,所有帖子页面应该只显示未归档的帖子。 But, I don't know how efficient it would be or if it is a decent option.但是,我不知道它的效率如何,或者它是否是一个不错的选择。

In databases, this is often called "soft-delete", and many systems do it - blogs hide outdated articles, on-line stores use it to hide products which are no longer offered, and so on.在数据库中,这通常被称为“软删除”,许多系统都这样做——博客隐藏过时的文章,在线商店使用它来隐藏不再提供的产品,等等。 It's a standard industry practice.这是标准的行业惯例。

Usually, you'll use the field to filter out posts when listing them.通常,您将在列出帖子时使用该字段过滤掉帖子。 At this point, you must make an important decision:此时,您必须做出重要决定:

  • Do you completely hide the "archived" articles and pretend they don't exist at all (404)?您是否完全隐藏“存档”文章并假装它们根本不存在(404)? Or...或者...
  • Do you hide them from listings only, but still keep references by _id functional, so that users' browser bookmarks continue to work?您是否仅将它们从列表中隐藏,但仍保留_id功能的引用,以便用户的浏览器书签继续工作?
  • Or maybe your website should have a separate section for "archived" posts that's still browsable?或者,也许您的网站应该有一个单独的部分用于“存档”帖子,这些帖子仍然可以浏览?

Once you've decided that, you will know which queries to modify in order to include the extra condition.一旦你决定了,你就会知道要修改哪些查询以包含额外的条件。

When it comes to performance, assuming your site has only a handful of posts (say, up to 1000), it will not matter at all - the bottleneck will usually be sorting, so make sure you have all the proper indexes (say, on a date field - see Mongo docs on indexing if in doubt).在性能方面,假设您的站点只有少数帖子(例如,最多 1000 个),这根本不重要 - 瓶颈通常是排序,因此请确保您拥有所有正确的索引(例如,在日期字段 - 如果有疑问,请参阅有关索引的 Mongo 文档)。

If you're going to have huge numbers of posts (in the millions), and the archived posts become a significant fraction of all posts (say, 50%), then you could use partial indexes to speed up queries - the index can hold only non-archival posts, with the archived posts thrown out of the index to save on index size (traversal time + RAM).如果您将拥有大量帖子(数百万),并且存档帖子占所有帖子的很大一部分(例如,50%),那么您可以使用部分索引来加速查询 - 索引可以容纳仅非归档帖子,将归档帖子从索引中抛出以节省索引大小(遍历时间 + RAM)。 Don't worry about this too soon, however - such optimizations may never be necessary on most sites.不过,不要过早地担心这一点——在大多数网站上可能永远不需要这样的优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM