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

The three-step find-edit-save approach


This approach requires a few more steps and a bit more code than the helper methods but is more fully featured. The save() command accepts only one parameter, a callback function to run once the save has completed. This callback function can pass an error or return the saved object. Look at the following example:

// 1: FIND the record
User.findOne(
  {email : 'simon@theholmesoffice.com'},
  function(err, user) {
    if(!err){
      // 2: EDIT the record
      user.name = "Simon";
      // 3: SAVE the record
      user.save(function(err,user){
        console.log('User saved:', user);
      });
    }
  };
);

As you can see there is a bit more code required, and there's also an extra callback level. However, the code remains pretty simple to follow. To make it even easier, you can also define the callbacks as separate functions. We'll see this later.

lock icon The rest of the chapter is locked
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}