Modeling Mongo Documents With Mongoose

Without a doubt, one of the quickest ways to build an application that leverages MongoDB is with Node. It’s as if the two platforms were made for each other; the sheer number of Node libraries available for dealing with Mongo is testimony to a vibrant, innovative community. Indeed, one of my favorite Mongo focused libraries these days is Mongoose.

Briefly, Mongoose is an object modeling framework that makes it incredibly easy to model collections and ultimately work with intuitive objects that support a rich feature set. Like most things in Node, it couldn’t be any easier to get set up. Essentially, to use Mongoose, you’ll need to define Schema objects – these are your documents – either top level or even embedded.

For example, I’ve defined a words collection that contains documents (representing…words) that each contain an embedded collection of definition documents. A sample document looks like this:

{ _id: '4fd7c7ac8b5b27f21b000001', spelling: 'drivel', synonyms: ['garbage', 'dribble', 'drool'], definitions: [ { part_of_speech: 'noun', definition:'saliva flowing from the mouth, or mucus from the nose; slaver.' }, { part_of_speech: 'noun', definition:'childish, silly, or meaningless talk or thinking; nonsense; twaddle.' }] }

From an document modeling standpoint, I’d like to work with a Word object that contains a list of Definition objects and a number of related attributes (i.e. synonyms, parts of speech, etc). To model this relationship with Mongoose, I’ll need to define two Schema types and I’ll start with the simplest:

Definition = mongoose.model 'definition', new mongoose.Schema({ part_of_speech : { type: String, required: true, trim: true, enum: ['adjective', 'noun', 'verb', 'adverb'] }, definition : {type: String, required: true, trim: true} })

As you can see, a Definition is simple – the part_of_speech attribute is an enumerated String that’s required; what’s more, the definition attribute is also a required String.

Next, I’ll define a Word:

Word = mongoose.model 'word', new mongoose.Schema({ spelling : {type: String, required: true, trim: true, lowercase: true, unique: true}, definitions : [Definition.schema], synonyms : [{ type: String, trim: true, lowercase: true }] })

As you can see, a Word instance embeds a collection of Definitions. Here I’m also demonstrating the usage of lowercase and the index unique placed on the spelling attribute.

To create a Word instance and save the corresponding document couldn’t be easier. Mongo array’s leverage the push command and Mongoose follows this pattern to the tee.

word = new models.Word({spelling : 'loquacious'}) word.synonyms.push 'verbose' word.definitions.push {definition: 'talking or tending to talk much or freely; talkative; \ chattering; babbling; garrulous.', part_of_speech: 'adjective' } (err, data) ->

Finding a word is easy too:

it 'findOne should return one', (done) -> models.Word.findOne spelling:'nefarious', (err, document) -> document.spelling.should.eql 'nefarious' document.definitions.length.should.eql 1 document.synonyms.length.should.eql 2 document.definitions[0]['part_of_speech'].should.eql 'adjective' done(err)

In this case, the above code is a Mocha test case (which uses should for assertions) that demonstrates Mongoose’s findOne.

You can find the code for these examples and more at my Github repo dubbed Exegesis and while you’re at it, check out the developerWorks videos I did for Node!

Reference: Modeling Mongo Documents With Mongoose from our JCG partner Andrew Glover at the The Disco Blog blog.


Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Leave a Reply

2 + = eleven

Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: