Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mongoose for Application Development

You're reading from  Mongoose for Application Development

Product type Book
Published in Aug 2013
Publisher Packt
ISBN-13 9781782168195
Pages 142 pages
Edition 1st Edition
Languages
Author (1):
Simon Holmes Simon Holmes
Profile icon Simon Holmes

Table of Contents (18) Chapters

Mongoose for Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Introducing Mongoose to the Technology Stack Establishing a Database Connection Schemas and Models Interacting with Data – an Introduction Interacting with Data – Creation Interacting with Data – Reading, Querying, and Finding Interacting with Data – Updating Interacting with Data – Deleting Validating Data Complex Schemas Plugins – Re-using Code Index

Chapter 4. Interacting with Data – an Introduction

We have seen how to define our data structure, but now we want to interact with it. We want to be able to create database entries, read them, update them, and delete them. To handle these interactions, Mongoose uses methods added to models. This chapter introduces the concept and will look at how to organize our code to best work with this approach.

By the end of this chapter, you will have had a glimpse of how Mongoose helps you to easily interact with your data. You will also have prepared the sample project so that we can dive into practical examples in later chapters.

Model methods and instance methods


As we have seen in Chapter 3, Schemas and Models, a document in a Mongoose collection is a single instance of a model. So it makes sense that if we're going to work with our data then it will be through the model.

So what do we mean by model methods? Well, if we have a model called User, some of the methods provided by Mongoose are User.create, User.find, User.update, and User.remove. The method names may be slightly different, but out of the box we've got methods for all four CRUD (Create, Read, Update, and Delete) operations right there.

Instance methods are the same concept, except that they are applied to specific instances instead. We will look at a good example of this soon, with the instance.save method.

Mongoose also allows you to define your own model methods, giving you the flexibility to create the functionality you want. We'll see more about these static methods and create one ourselves in Chapter 6, Interacting with Data – Reading, Querying, and...

Setting up the project


Before we get going on the Mongoose code, we need to get a couple of things in order in our project:

  • Our approach to code structure

  • The URLs/routes we'll need

Code structure

If we think in an MVC way, our Express project already has a model folder and a views folder. The model folder holds our schema definitions, Mongoose models and Mongoose connection. The views folder contains Jade templates for page layout. The controller aspect sits in the routes folder of a standard express installation. We could separate it out or move it around, but as the focus of this book is on using Mongoose, but not on MVC best practices, let's work with the default setup.

Adding the routes files

A good starting point when thinking about setting up your routes is to have one routes file for each Mongoose model. Given the non-normalized data structure of MongoDB there aren't generally too many of these—we're not talking about individual tables in a relational database here! Also, as we saw earlier...

Summary


In this chapter, we have had a very quick glance at how Mongoose provides methods for us to interact with our data. We have also seen how to set up our project to work with Mongoose, attaching our schemas and models to the actual back end of the application.

In the next four chapters, we will be exploring the Mongoose data methods in much greater detail, focusing around the cornerstones of CRUD operations. First up, Chapter 5, Interacting with Data – Creation, for creating data.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Mongoose for Application Development
Published in: Aug 2013 Publisher: Packt ISBN-13: 9781782168195
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}