HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Seeding MongoDB using Mongoose models

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
mongooseseedingmongodbusingmodels

Problem

I'm using mongoose to seed a mongodb database based on my mongoose models.
I'm particular interested in improving the seed code.

I have two models, an article and a category.
The relationship between the models are as follows:

A category has many articles
An article has a parent category

The model schemas I'm using are listed below:

The category schema

var mongoose = require('mongoose');

var categorySchema = mongoose.Schema({
    title: String,
    articles : [{ type: mongoose.Schema.Types.ObjectId, ref: 'Article' }]
});

mongoose.model('Category', categorySchema);


The article schema

var mongoose = require('mongoose');

var articleSchema = mongoose.Schema({
    category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' },
    title: String
});

mongoose.model('Article', articleSchema);


I'm using the following code to seed:

Seeding the category

var CategoryModel = require('mongoose').model('Category');

exports.seedCategories = function seedCategories() {
    CategoryModel.find({}).exec(function (err, collection) {
        if (collection.length === 0) {
            CategoryModel.create({ title: 'Category One' });
            CategoryModel.create({ title: 'Category Two' });
        }
    });
}


Seeding the article

```
var CategoryModel = require('mongoose').model('Category');
var ArticleModel = require('mongoose').model('Article');

exports.seedArticles = function seedArticles() {

ArticleModel.find({}).exec(function (err, collection) {
if (collection.length === 0) {
seedArticle('Category One', 'Article One');
seedArticle('Category One', 'Article Two');
seedArticle('Category Two', 'Article Three');
seedArticle('Category Two', 'Article Four');
}
});

function seedArticle(categoryTitle, articleTitle) {
var parentCategory;
CategoryModel.findOne({ title: categoryTitle }).exec()
.then(function (category) {
paren

Solution

It seems that you are storing the data with double links (article -> category AND category -> articles).

I assume that you need to report on articles for a category. I would simple create an index on Category like this:

var articleSchema = mongoose.Schema({
    category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', index: true },
    title: String
});


This way you can simplify your code a ton by keeping it KISS:

function seedArticle(categoryTitle, articleTitle) {
  CategoryModel.findOne({ title: categoryTitle }).exec()
    .then(function (category) {
      return ArticleModel.create({ title: articleTitle, category: category._id });
    });
}


Other than that, I like your code.

Code Snippets

var articleSchema = mongoose.Schema({
    category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', index: true },
    title: String
});
function seedArticle(categoryTitle, articleTitle) {
  CategoryModel.findOne({ title: categoryTitle }).exec()
    .then(function (category) {
      return ArticleModel.create({ title: articleTitle, category: category._id });
    });
}

Context

StackExchange Code Review Q#42171, answer score: 5

Revisions (0)

No revisions yet.