Table of Contents for
React Quickly: Painless web apps with React, JSX, Redux, and GraphQL

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition React Quickly: Painless web apps with React, JSX, Redux, and GraphQL by Azat Mardan Published by Manning Publications, 2017
  1. Cover
  2. React Quickly: Painless web apps with React, JSX, Redux, and GraphQL
  3. Copyright
  4. React Quickly: Painless web apps with React, JSX, Redux, and GraphQL
  5. Brief Table of Contents
  6. Table of Contents
  7. Praise for React Quickly
  8. Foreword
  9. Preface
  10. Acknowledgments
  11. About This Book
  12. About the Author
  13. About the Cover
  14. Part 1. React foundation
  15. Chapter 1. Meeting React
  16. Chapter 2. Baby steps with React
  17. Chapter 3. Introduction to JSX
  18. Chapter 4. Making React interactive with states
  19. Chapter 5. React component lifecycle events
  20. Chapter 6. Handling events in React
  21. Chapter 7. Working with forms in React
  22. Chapter 8. Scaling React components
  23. Chapter 9. Project: Menu component
  24. Chapter 10. Project: Tooltip component
  25. Chapter 11. Project: Timer component
  26. Part 2. React architecture
  27. Chapter 12. The Webpack build tool
  28. Chapter 13. React routing
  29. Chapter 14. Working with data using Redux
  30. Chapter 15. Working with data using GraphQL
  31. Chapter 16. Unit testing React with Jest
  32. Chapter 17. React on Node and Universal JavaScript
  33. Chapter 18. Project: Building a bookstore with React Router
  34. Chapter 19. Project: Checking passwords with Jest
  35. Chapter 20. Project: Implementing autocomplete with Jest, Express, and MongoDB
  36. Appendix A. Installing applications used in this book
  37. Appendix B. React cheatsheet
  38. Appendix C. Express.js cheatsheet
  39. Appendix D. MongoDB and Mongoose cheatsheet
  40. Appendix E. ES6 for success
  41. React Cheatsheet
  42. Index
  43. List of Figures
  44. List of Tables
  45. List of Listings

Appendix D. MongoDB and Mongoose cheatsheet

When you develop your own projects, searching on the internet for React documentation and APIs or going back to this book’s chapters to find a single method isn’t efficient. If you’d like to save time and avoid the distractions lurking everywhere on the Net, use this MongoDB cheatsheet as a quick reference.

Print-ready PDF available

In addition to the text version presented here, I’ve created a free beautifully designed, print-ready PDF version of this cheatsheet. You can request this PDF at http://reactquickly.co/resources.

MongoDB

  • $ mongod—Starts the MongoDB server (localhost:27017)
  • $ mongo (connects to the local server by default)—Opens the MongoDB console

MongoDB console

  • > show dbs—Shows databases on the server
  • > use DB_NAME—Selects the database DB_NAME
  • > show collections—Shows collections in the selected database
  • > db.COLLECTION_NAME.find()—Performs a find query on the collection named COLLECTION_NAME to find any items
  • > db.COLLECTION_NAME.find({"_id"—ObjectId("549d9a3081d0f07866fdaac6") })—Performs a find query on the collection named COLLECTION_NAME to find the item with ID 549d9a3081d0f07866fdaac6
  • > db.COLLECTION_NAME.find({"email": /gmail/})—Performs a find query on the collection named COLLECTION_NAME to find items with an email property matching /gmail
  • > db.COLLECTION_NAME.update(QUERY_OBJECT, SET_OBJECT)—Performs an update query on the collection named COLLECTION_NAME to update items that match QUERY_OBJECT with SET_OBJECT
  • > db.COLLECTION_NAME.remove(QUERY_OBJECT)—Performs a remove query for items matching QUERY_OBJECT criteria on the COLLECTION_NAME collection
  • > db.COLLECTION_NAME.insert(OBJECT)—Adds OBJECT to the collection named COLLECTION_NAME

Installing Mongoose

  • $ sudo npm install mongoose—Installs the latest version of Mongoose locally
  • $ sudo npm install mongoose@3.8.20 --save—Installs Mongoose v3.8.20 locally, and saves it to package.json

Mongoose basic usage

var mongoose = require('mongoose')
var dbUri = 'mongodb://localhost:27017/api'
var dbConnection = mongoose.createConnection(dbUri)
var Schema = mongoose.Schema
var postSchema = new Schema ({
  title: String,
  text: String
})
var Post = dbConnection.model('Post', postSchema, 'posts')
Post.find({},function(error, posts){
  console.log(posts)
  process.exit(1)
})

Mongoose schema

  • String
  • Boolean
  • Number
  • Date
  • Array
  • Buffer
  • Schema.Types.Mixed
  • Schema.Types.ObjectId

Create, read, update, delete (CRUD) Mongoose example

// Create
var post = new Post({title: 'a', text: 'b')
post.save(function(error, document){
  ...
})
// Read
Post.findOne(criteria, function(error, post) {
  ...
})
// Update
Post.findOne(criteria, function(error, post) {
  post.set()
  post.save(function(error, document){
    ...
  })
})
// Delete
Post.findOne(criteria, function(error, post) {
  post.remove(function(error){
    ...
  })
})

Mongoose model methods

  • find(criteria, [fields], [options], [callback]), where callback has error and documents arguments—Finds a document
  • count(criteria, [callback])), where callback has error and count arguments—Returns a count of documents with matching criteria
  • findById(id, [fields], [options], [callback]), where callback has error and document arguments—Returns a single document by ID
  • findByIdAndUpdate(id, [update], [options], [callback])—Executes MongoDB’s findAndModify() to update a document by ID
  • findByIdAndRemove(id, [options], [callback])—Executes MongoDB’s findAndModify() to remove a document by ID
  • findOne(criteria, [fields], [options], [callback]), where callback has error and document arguments—Returns a single document
  • findOneAndUpdate([criteria], [update], [options], [callback])—Executes MongoDB’s findAndModify() to update document(s)
  • findOneAndRemove(id, [update], [options], [callback])—Executes MongoDB’s findAndModify() to remove a document
  • update(criteria, update, [options], [callback]), where callback has error and count arguments—Updates documents
  • create(doc(s), [callback]), where callback has error and doc(s) arguments—Creates a document object, and saves it to the database
  • remove(criteria, [callback]), where callback has an error argument—Removes documents

Mongoose document methods

  • save([callback]), where callback has error, doc, and count arguments—Saves the document
  • set(path, val, [type], [options])—Sets a value on the document’s property
  • get(path, [type])—Gets the value of a property
  • isModified([path])—Checks whether a property has been modified
  • populate([path], [callback])—Populates a reference
  • toJSON(options)—Gets JSON from the document
  • validate(callback)—Validates the document