Home Page Icon
Home Page
Table of Contents for
Chapter 4: Working out your Way with Queries
Close
Chapter 4: Working out your Way with Queries
by Gautam Rege
Ruby and MongoDB Web Development Beginner's Guide
Ruby and MongoDB Web Development Beginner's Guide
Ruby and MongoDB Web Development Beginner's Guide
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
Understanding NoSQL
Brewer's CAP theorem
What are BASE databases?
Using ACID or BASE?
Using Ruby
What this book covers
What you need for this book
Who this book is for
Conventions
Time for action — heading
What just happened?
Pop quiz — heading
Have a go hero — heading
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Installing MongoDB and Ruby
Installing Ruby
Using RVM on Linux or Mac OS
The RVM games
The Windows saga
Using rbenv for installing Ruby
Installing MongoDB
Configuring the MongoDB server
Starting MongoDB
Stopping MongoDB
The MongoDB CLI
Understanding JavaScript Object Notation (JSON)
Connecting to MongoDB using Mongo
Saving information
Retrieving information
Deleting information
Exporting information using mongoexport
Importing data using mongoimport
Managing backup and restore using mongodump and mongorestore
Saving large files using mongofiles
bsondump
Installing Rails/Sinatra
Summary
2. Diving Deep into MongoDB
Creating documents
Time for action — creating our first document
What just happened?
NoSQL scores over SQL databases
Using MongoDB embedded documents
Time for action — embedding reviews and votes
What just happened?
Have a go hero — adding more embedded objects to the book
Fetching embedded objects
Using MongoDB document relationships
Time for action — creating document relations
What just happened?
Comparing MongoDB versus SQL syntax
Using Map/Reduce instead of join
Understanding functional programming
Building the map function
Time for action — writing the map function for calculating vote statistics
What just happened?
Building the reduce function
Time for action — writing the reduce function to process emitted information
What just happened?
Understanding the Ruby perspective
Setting up Rails and MongoDB
Time for action — creating the project
What just happened?
Understanding the Rails basics
Using Bundler
Why do we need the Bundler
Setting up Sodibee
Time for action — start your engines
What just happened?
Setting up Mongoid
Time for action — configuring Mongoid
What just happened?
Building the models
Time for action — planning the object schema
Building the book model
What just happened?
Have a go hero — building the remaining models
Testing from the Rails console
Time for action — putting it all together
What just happened?
Have a go hero — adding more books, authors, and categories
Understanding many-to-many relationships in MongoDB
Using embedded documents
Time for action — adding reviews to books
What just happened?
Choosing whether to embed or not to embed
Time for action — embedding Lease and Purchase models
Working with Map/Reduce
Time for action — writing the map function to calculate ratings
What just happened?
Time for action — writing the reduce function to process the emitted results
What just happened?
Using Map/Reduce together
Time for action — working with Map/Reduce using Ruby
What just happened?
Pop quiz — swimming in MongoDB and Ruby
Summary
3. MongoDB Internals
Understanding Binary JSON
Fetching and traversing data
Manipulating data
What is ObjectId?
Documents and collections
Capped collections
Dates in MongoDB
JavaScript and MongoDB
Time for action — writing our own custom functions in MongoDB
Ensuring write consistency or "read your writes"
How does MongoDB use its memory-mapped storage engine?
Advantages of write-ahead journaling
Global write lock
Transactional support in MongoDB
Understanding embedded documents and atomic updates
Implementing optimistic locking in MongoDB
Time for action — implementing optimistic locking
What just happened?
Choosing between ACID transactions and MongoDB transactions
Why are there no joins in MongoDB?
Pop quiz — the dos and don'ts of MongoDB
Summary
4. Working Out Your Way with Queries
Searching by fields in a document
Time for action — searching by a string value
What just happened?
Have a go hero — search for books from an author
Querying for specific fields
Time for action — fetching only for specific fields
What just happened?
Have a go hero — including and excluding fields
Using skip and limit
Time for action — skipping documents and limiting our search results
What just happened?
Have a go hero — paginating document results
Writing conditional queries
Using the $or operator
Time for action — finding books by name or publisher
What just happened?
Writing threshold queries with $gt, $lt, $ne, $lte, and $gte
Time for action — finding the highly ranked books
Have a go hero — find books via rank
Checking presence using $exists
Searching inside arrays
Time for action — searching inside reviews
What just happened?
Searching inside arrays using $in and $nin
Searching for exact matches using $all
Searching inside hashes
Searching inside embedded documents
Searching with regular expressions
Time for action — using regular expression searches
What just happened?
Have a go hero — validate an e-mail address
Pop quiz — searching the right way
Summary
5. Ruby DataMappers: Ruby and MongoDB Go Hand in Hand
Why do we need Ruby DataMappers
The mongo-ruby-driver
Time for action — using mongo gem
What just happened?
The Ruby DataMappers for MongoDB
MongoMapper
Mongoid
Setting up DataMappers
Configuring MongoMapper
Time for action — configuring MongoMapper
What just happened?
Have a go hero — creating models using MongoMapper
Configuring Mongoid
Time for action — setting up Mongoid
What just happened?
Creating, updating, and destroying documents
Defining fields using MongoMapper
Defining fields using Mongoid
Creating objects
Time for action — creating and updating objects
What just happened?
Using finder methods
Using find method
Using the first and last methods
Using the all method
Using MongoDB criteria
Executing conditional queries using where
Time for action — fetching using the where criterion
What just happened?
Revisiting limit, skip, and offset
Understanding model relationships
The one to many relation
Time for action — relating models
Using MongoMapper
Using Mongoid
What just happened?
The many-to-many relation
Time for action — categorizing books
MongoMapper
Mongoid
Accessing many-to-many with MongoMapper
Accessing many-to-many relations using Mongoid
What just happened?
The one-to-one relation
Using MongoMapper
Using Mongoid
Time for action — adding book details
What just happened?
Have a go hero — create the other models
Understanding polymorphic relations
Implementing polymorphic relations the wrong way
Implementing polymorphic relations the correct way
Single Collection Inheritance
Time for action — managing the driver entities
What just happened?
Basic polymorphic relations
Time for action — creating vehicles using basic polymorphism
What just happened?
Choosing SCI or basic polymorphism
Using embedded objects
Time for action — creating embedded objects
Using MongoMapper
Using Mongoid
What just happened?
Using MongoMapper
Using Mongoid
Reverse embedded relations in Mongoid
Time for action — using embeds_one without specifying embedded_in
What just happened?
Time for action — using embeds_many without specifying embedded_in
What just happened?
Understanding embedded polymorphism
Single Collection Inheritance
Time for action — adding licenses to drivers
What just happened?
Basic embedded polymorphism
Time for action — insuring drivers
What just happened?
Have a go hero
Choosing whether to embed or to associate documents
Mongoid or MongoMapper — the verdict
Pop quiz — Mongoid, MongoMapper, and more
Summary
6. Modeling Ruby with Mongoid
Developing a web application with Mongoid
Setting up Rails
Time for action — setting up a Rails project
What just happened?
Setting up Sinatra
Time for action — using Sinatra professionally
What just happened?
Understanding Rack
Defining attributes in models
Accessing attributes
Indexing attributes
Unique indexes
Background indexing
Geospatial indexing
Sparse indexing
Dynamic fields
Time for action — adding dynamic fields
What just happened?
Localization
Time for action — localizing fields
What just happened?
Using arrays and hashes in models
Embedded objects
Defining relations in models
Common options for all relations
:class_name option
:inverse_of option
:name option
Relation-specific options
Options for has_one
:as option
:autosave option
:dependent option
:foreign_key option
Options for has_many
:order option
Options for belongs_to
:index option
:polymorphic option
Options for has_and_belongs_to_many
:inverse_of option
Time for action — configuring the many-to-many relation
What just happened?
Time for action — setting up the following and followers relationship
What just happened?
Options for :embeds_one
:cascade_callbacks option
:cyclic
Time for action — setting up cyclic relations
What just happened?
Options for embeds_many
:versioned option
Options for embedded_in
Have a go hero — embedded polymorphic relations
:name option
Managing changes in models
Time for action — changing models
What just happened?
Mixing in Mongoid modules
The Paranoia module
Time for action — getting paranoid
What just happened?
Versioning
Time for action — including a version
What just happened?
Pop quiz — dancing with Mongoid models
Summary
7. Achieving High Performance on Your Ruby Application with MongoDB
Profiling MongoDB
Time for action — enabling profiling for MongoDB
What just happened?
Using the explain function
Time for action — explaining a query
What just happened?
Using covered indexes
Time for action — using covered indexes
What just happened?
Other MongoDB performance tuning techniques
Using mongostat
Understanding web application performance
Web server response time
Throughput
Load the server using httperf
Monitoring server performance
Average response time
Concurrency/throughput
Apdex Score
End-user response and latency
Optimizing our code for performance
Indexing fields
Optimizing data selection
Optimizing and tuning the web application stack
Performance of the memory-mapped storage engine
Choosing the Ruby application server
Passenger
Mongrel and Thin
Unicorn
Increasing performance of Mongoid using bson_ext gem
Caching objects
Memcache
Redis server
Summary
8. Rack, Sinatra, Rails, and MongoDB — Making Use of them All
Revisiting Sodibee
The Rails way
Setting up the project
Modeling Sodibee
Time for action — modeling the Author class
What just happened?
Time for action — writing the Book, Category and Address models
What just happened?
Time for action — modeling the Order class
What just happened?
Understanding Rails routes
What is the RESTful interface?
Time for action — configuring routes
What just happened?
Understanding the Rails architecture
Processing a Rails request
Coding the Controllers and the Views
Time for action — writing the AuthorsController
What just happened?
Solving the N+1 query problem using the includes method
Relating models without persisting them
Designing the web application layout
Time for action — designing the layout
What just happened?
Understanding the Rails asset pipeline
Designing the Authors listing page
Time for action — listing authors
What just happened?
Adding new authors and their books
Time for action — adding new authors and books
What just happened?
Have a go hero
The Sinatra way
Time for action — setting up Sinatra and Rack
What just happened?
Have a go hero
Testing and automation using RSpec
Understanding RSpec
Time for action — installing RSpec
What just happened?
Time for action — sporking it
What just happened?
Have a go hero
Documenting code using YARD
Pop quiz — it's all about the web
Summary
9. Going Everywhere — Geospatial Indexing with MongoDB
What is geolocation
How accurate is a geolocation
Converting geolocation to geocoded coordinates
Identifying the exact geolocation
Storing coordinates in MongoDB
Time for action — geocoding the Address model
What just happened?
Testing geolocation storage
Time for action — saving geolocation coordinates
What just happened?
Using geocoder to update coordinates
Time for action — using geocoder for storing coordinates
What just happened?
Firing geolocation queries
Time for action — finding nearby addresses
What just happened?
Using mongoid_spacial
Time for action — firing near queries in Mongoid
What just happened?
Differences between $near and $geoNear
Summary
10. Scaling MongoDB
High availability and failover via replication
Implementing the master/slave replication
Time for action — setting up the master/slave replication
What just happened?
Using replica sets
Time for action — implementing replica sets
What just happened?
Recovering from crashes — failover
Adding members to the replica set
Implementing replica sets for Sodibee
Time for action — configuring replica sets for Sodibee
What just happened?
Implementing sharding
Creating the shards
Time for action — setting up the shards
What just happened?
Configuring the shards with a config server
Time for action — starting the config server
What just happened?
Setting up the routing service — mongos
Time for action — setting up mongos
What just happened?
Testing shared replication
Implementing Map/Reduce
Time for action — planning the Map/Reduce functionality
What just happened?
Time for action — Map/Reduce via the mongo console
What just happened?
Time for action — Map/Reduce via Ruby
What just happened?
Performance benchmarking
Time for action — iterating Ruby objects
What just happened?
Pop quiz — scaling our web app
Summary
A. Pop quiz — Answers
Chapter 2: Diving Deep into MongoDB
Chapter 3: MongoDB Internals
Chapter 4: Working out your Way with Queries
Chapter 5: Ruby DataMappers: Ruby and MongoDB Go Hand in Hand
Chapter 6: Modeling Ruby with Mongoid
Chapter 8: Rack, Sinatra, Rails and MongoDB - Making use of them all
Chapter 10: Scaling MongoDB
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
Chapter 3: MongoDB Internals
Next
Next Chapter
Chapter 5: Ruby DataMappers: Ruby and MongoDB Go Hand in Hand
Chapter 4: Working out your Way with Queries
1
2
3
4
b
a
d
b
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset