Quinton Pike
Quinton Pike

Reputation: 3861

Node.js - Mongoose/MongoDB - Model Schema

I am creating a blog system in Node.js with mongodb as the db.

I have contents like this: (blog articles):

// COMMENTS SCHEMA:
// ---------------------------------------
var Comments = new Schema({

    author: {
        type: String
    },
    content: {
        type: String
    },
    date_entered: {
        type: Date,
        default: Date.now
    }

});
exports.Comments = mongoose.model('Comments',Comments);
var Tags = new Schema({ 
    name: {
        type: String
    } 
});
exports.Tags = mongoose.model('Tags',Tags);


// CONTENT SCHEMA:
// ---------------------------------------
exports.Contents = mongoose.model('Contents', new Schema({

    title: {
        type: String
    },
    author: {
        type: String
    },
    permalink: {
        type: String, 
        unique: true,
        sparse: true
    },
    catagory: {
        type: String,
        default: ''
    },
    content: {
        type: String
    }, 
    date_entered: {
        type: Date,
        default: Date.now
    }, 
    status: {
        type: Number
    },
    comments: [Comments],
    tags: [Tags]

}));

I am a little new to this type of database, im used to MySQL on a LAMP stack.

Basically my question is as follows:

In MYSQL we would have a tags table and a categories table and relate by keys, I am not sure the best and most optimal way of doing it in Mongo.

THANK YOU FOR YOUR TIME!!

Upvotes: 1

Views: 2993

Answers (2)

Krishna Sankar
Krishna Sankar

Reputation: 3807

Couple of ideas for Mongo:

  • The best way to associate a user is e-mail address - as an attribute of the content/comment document - e-mail is usually a reliable unique key. MongoDB doesn't have foreign keys or associated constraints. But that is fine.
  • If you have a registration policy, add user name, e-mail address and other details to the users collection. Then de-normalize the content document with the user name and e-mail. If, for any reason, the user changes the name, you will have to update all the associated contents/comments. But so long as the e-mail address is there in the documents, this should be easy.
  • Tags and categories are best modelled as two lists in the content document, IMHO.
  • You can also create two indices on these attributes, if required. Depends on the access patterns and the UI features you want to provide
  • You can also add a document which keeps a tag list and a categories list in the contents collection and use $addToSet to add new tags and categories to this document. Then, you can show a combo box with the current tags as a starting point.
  • As a final point, think through the ways you plan to access the data and then design documents, collections & indices accordingly
  • [Update 12/9/11] Was at MongoSv and Eliot (CTO 10gen) presented a pattern relevant to this question: Instead of one comment document per user (which could grow large) have a comment document per day for a use with _id = -YYYYMMDD or even one per month depending on the frequency of comments. This optimizes index creation/document growth vs document proliferation (in case of the design where there is one comment per user).

Upvotes: 2

Amol M Kulkarni
Amol M Kulkarni

Reputation: 21629

  1. The best way to associate the Content Authors to a User in the MongoDB, is to take an array in Author Collection which keeps an reference to User. Basically Array because One Content/Book may have multiple Authors i.e. you need to associate one Content to many Users.

  2. The best way for category is to create a different collection in your DB and similarly as above keep a array in Contents.

I hope it helps at-least a little.

Upvotes: 0

Related Questions