Skip to content
Robert edited this page Apr 16, 2015 · 2 revisions

Below are the schemas for the articles and journals collections stored in MongoDB. Each schema maps to the relevant collection and uses is created with the help of Mongoose.

Mongo's ObjectId is used by default for the _id field.

##ArticleModel schema##

var articleSchema = new Schema({
        title: { type: String, index:true},
        doi: { type: String, index:true},
        pmid: {type: Number, index: true},
        pmcid: String,
        author: String,
        license: String,
        references: [{ type: ObjectId, ref: "ArticleModel" }],
        is_ref_of: [{ type: ObjectId, ref: "ArticleModel"}],
        journal: {type: ObjectId, ref: "JournalModel"},
        volume: Number,
        date: Date,
        year: String,
        link: String,
        spage: String,
        free_access: Boolean,
        abstract: String,
        citation_count: Number,
        citation_count_at_two: Number,
        citation_group: String,
        reference_count: Number,
        group: String,
        sub_group: String	
     });

ArticleModel contains a references and an is_ref_of (probably should've been called citations) field of type array. Each element of the array is the ObjectId of another document in the articlemodels collection.

The journal field is a reference to a journal in the journalmodel collection.

##JournalModel schema##

var journalSchema = new Schema({
    title : {type: String, index: true},
    mapping: {type: String, index: true},
    free_access: Boolean,
    publisher: String,
    articles: [{type: ObjectId, ref: 'ArticleModel'}],
    issn : String
});

Similarly, JournalModel has an articles field which holds references to articles in the articles collection.

##Why Mongoose?##

Mongoose helps with modelling application data and handles object document mapping. It's a nice way to interact with MongoDB.

Because there are no joins in MongoDB (yeah I know - a data model with lots of joins...) Mongoose has a populate method that automatically replaces the specified paths with document(s) from other collections.

For example this is the query that pmc-ref uses to return references that are free to access in PMC when /api/v1/references/free/ is hit.

 ArticleModel.find({doi: doi})
			.populate({
				path: "references",
				match: {free_access: true},
				select:"-_id -is_ref_of -__v -references -abstract -journal"
			})
			.populate({path: "journal", select: "title"})
			.select("-_id -is_ref_of -__v")
			.exec(function(error, article){
				if(error){
					response.json(500, {error: error.message});
				} else{
					response.json(200, {article: article});
				}
			});

See it in action

code in repo

Calling the populate method on the query returns an array of documents in place of the _ids.

And this is the one that returns all a journal's articles:

JournalModel.find({title: title})
			.populate({path: "articles",
						select: "title author doi free_access pmcid -_id"})
			.select("-_id -__v")
			.exec(function(err, journal){
				if(err){
					response.json(500, {error: err.message});
				}else{
					response.json(200, {journal: journal} );
				}
			});
};

See it in action

Clone this wiki locally