Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Change of schema in production

SoftTimur:

In the version 1 of my mean-stack website, I have defined a schema as follows:


var PostSchema = new mongoose.Schema({
title: { type: String, default: "DefaultTitle" },
author: { type: String, default: "DefaultAuthor" },
... ...
})

Now, I realize that, to search all the posts written by an Author, I cannot rely on their name in String (because two authors may have same name); I should rely on the _id of author. Thus, I need to at least add _id of author in PostSchema. So there are several choices for the new PostSchema:

1) with this schema, I need to populate the content of authors, whereas what I need is just the name of the author, I think it is not very effective.


var PostSchema = new mongoose.Schema({
author: { type: mongoose.Schema.Types.ObjectId, ref: 'Author' }

2) with this schema, before using a post, I need to find the name of the author by id, and update the name of the post (because the name of an author may change). But is it a good idea to have name in the author object or separate author name and id in PostSchema like 3)?


var PostSchema = new mongoose.Schema({
author: { id: String, name: String }

3)


var PostSchema = new mongoose.Schema({
author: { type: String, default: "DefaultAuthor" },
authorId: String

Could anyone tell me which design is the best and flexible for future change? I have already a database of authors and posts in the production, so I also need to make sure the change of schema does not cause too much manual modification of the existing documents.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Change of schema in production

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×