background preloader

MONGO-DB

Facebook Twitter

Ruby, Rails, MongoDB and the Object-Relational Mismatch. By Emily Stolfo, Ruby Engineer and Evangelist at 10gen MongoDB is a popular choice among developers in part because it permits a one-to-one mapping between object-oriented (OO) software objects and database entities.

Ruby, Rails, MongoDB and the Object-Relational Mismatch

Ruby developers are at a great advantage in using MongoDB because they are already used to working with and designing software that is purely object-oriented. Most of the discussions I’ve had about MongoDB and Ruby assume Ruby knowledge and explain why MongoDB is a good fit for the Rubyist. This post will do the opposite; I’m going to assume you know a few things about MongoDB but not much about Ruby.

In showing the Rubyist’s OO advantage, I’ll share a bit about the Ruby programming language and its popularity, explain specifically how the majority of Ruby developers are using MongoDB, and then talk about the future of the 10gen Ruby driver in the context of the Rails community. Aggregation Framework Examples — MongoDB Manual 2.4.5. Counting Unique Items with Map-Reduce. Credit: Kyle Banker Problem You have a collection that stores pageviews by user, and you want to count the total number of unique user visits per day using map-reduce.

Counting Unique Items with Map-Reduce

Each pageview document looks something like this: The solution requires grouping the pageviews by day and then counting the total number of user visits and along with the number of unique visits for that day. Solution What's tricky about this situation is that it requires a two-pass map-reduce in order to scale well. 1. Map Step. Mongo. For the PHP Mind, Part 1. This is part one of a three part blog series by Mitch Pirtle.

for the PHP Mind, Part 1

We have covered a lot on the blog about MongoDB features, as well as many ways to utilize MongoDB from different languages. This is the first in a series of posts from the perspective of a PHP developer; and covers the gamut from getting started to advanced concepts. I’m not going to waste the first blog post getting you up and running with MongoDB and the PHP extension, as that whole process is documented quite beautifully: While we’re at it, you should also take advantage of the online documentation for MongoDB, as well as the reference for the MongoDB extension for PHP.

Getting Started. For the PHP Mind, Part 2. This is part 2 of a series, with part 1 covering the bare essentials to get you going.

for the PHP Mind, Part 2

In this post we are going to take a closer look at queries and how indexes work in MongoDB. Introduction I’d like to kick off this post with a thanks to the folks behind the PHP extension for MongoDB, who have done a fantastic job of matching the functionality of the Mongo shell client. This is important when you start to see how similarly the two function, and you might find that you can tweak your logic using the shell and quickly implement the same logic from within PHP. The PHP extension supports something that is rather new to a lot of folks in the PHP world, a feature called method chaining: The ability to run several methods at the same time on one object. $result = $songs->find()->limit(2); Of course this works in the Mongo shell too.

For the PHP Mind, Part 3. This is part 3 in a series, which will focus on the data modeling aspect of working with document databases.

for the PHP Mind, Part 3

The previous parts are also available for reading: Part 1: Getting Started, and Part 2: Queries and Indexes. The Usual Suspects Although there are plenty of existing articles, presentations and webcasts about modeling your data to take advantage of a document database, this post is taking a slightly PHP-centric position as a part of this series. This information is useful to anyone though, regardless of their chosen programming language. We’re going to use two different scenarios to look at data modeling in the document world, chosen as common examples to illustrate differences in implementation between relational and document databases: Blog. Scenario 1: Getting All Bloggy. MongoDB Gotchas & How To Avoid Them. N.b. this post has been updated as of July 29th, 2014 A lot of people hate on MongoDB.

MongoDB Gotchas & How To Avoid Them

In my opinion they’re misguided - the main reason so many people think like this is a lack of understanding. Everyone should be able to benefit from MongoDB’s power and simplicity, and so as a follow up to David’s article I have outlined some common and not-so-common things that hackers should know about MongoDB. First though, why should you listen to me? I used to be a consultant specializing in Ops and helping companies (The Guardian, Experian) scale large web applications. 32-bit vs 64-bit Most modern servers are either running 32-bit or 64-bit operating systems.

SQL to MongoDB Mapping Chart — MongoDB Manual 2.4.4. In addition to the charts that follow, you might want to consider the Frequently Asked Questions section for a selection of common questions about MongoDB.

SQL to MongoDB Mapping Chart — MongoDB Manual 2.4.4

The following table presents the various SQL statements and the corresponding MongoDB statements. The examples in the table assume the following conditions: Query Mongo: MySQL to MongoDB Query Translator. Things I wish I knew about MongoDB a year ago. I’ve used MongoDB for over a year at scale at both Heyzap and Bugsnag and I’ve found it to be a very capable database.

Things I wish I knew about MongoDB a year ago

As with all databases, there are some gotchas, and here is a summary of the things I wish someone had told me earlier. Selective counts are slow even if indexed For example, when paginating a users feed of activity, you might see something like, In MongoDB this count can take orders of magnitude longer than you would expect. There is an open ticket and is currently slated for 2.4, so here’s hoping they’ll get it out. Inconsistent reads in replica sets. Optimize Query Performance with Indexes and Projections — MongoDB Manual 2.4.3. Create Indexes to Support Queries For commonly issued queries, create indexes.

Optimize Query Performance with Indexes and Projections — MongoDB Manual 2.4.3

If a query searches multiple fields, create a compound index. Scanning an index is much faster than scanning a collection. The indexes structures are smaller than the documents reference, and store references in order. Example If you have a posts collection containing blog posts, and if you regularly issue a query that sorts on the author_name field, then you can optimize the query by creating an index on the author_name field: MongoDB shards and replication. I have been working on a project at work that uses MongoDB.

mongoDB shards and replication

Most of the documentation has been good, but there are times when we have needed to do something that is not covered. One of the recommendations that you will find is to have replica sets. Optimize Query Performance with Indexes and Projections — MongoDB Manual 2.4.3. Things I wish I knew about MongoDB a year ago. MongoDB Sharding Guide: Server Setup on Ubuntu + EC2 - Blog - Joslyn Esser. Now that we have the overview of sharding out of the way, it’s time to build the servers. I’ll be using EC2 and Ubuntu 9.10 in this guide.

If you’d like to skip the manual labor in this part of the guide, the complete build scripts are available on github . Shard Servers First, we’ll need to get our shards up and running. If you are deploying to EC2, make sure you have the following completed before you get started: Create two EBS boot EC2 instances using an up to date Ubuntu 9.10 AMI (as of this writing: ami-7d43ae14) Create and attach two EBS volumes to /dev/sdf for storing your data on each of the shards Change your security group to allow for the MongoDB ports we’ll be using (27017-27019) Once your instances are up and running, ssh in to your first shard server (Shard A) and perform the following: Add the 10gen MongoDB repository to /etc/apt/sources.list: deb 9.10 10gen. { Online Notes } » Blog Archive » ‘Mongos’ your MongoDB scharding hub.

Let’s get right into it. This is going to be a small tutorial on how to shard your data using MongoDB. So grab that cup of coffee, turn on some tunes, and let’s get cranking away. On-premises Deployment and Monitoring of MongoDB Sharded Clusters. Last year, we did a survey asking our users about any other databases they were using alongside MySQL.

A clear majority were interested in using other databases alongside MySQL, these included (in order of popularity) MongoDB, PostgreSQL, Cassandra, Hadoop, Redis. MongoDB: Architectural Best Practices. With the launch of our MongoDB solutions, developers can provision powerful, optimized, horizontally scaling NoSQL database clusters in real-time on bare metal infrastructure in SoftLayer data centers around the world. We worked tirelessly with our friends at 10gen — the creators of MongoDB — to build and tweak hardware and software configurations that enable peak MongoDB performance, and the resulting platform is pretty amazing. As Duke mentioned in his blog post, those efforts followed 10Gen's MongoDB best practices, but what he didn't mention was that we created some architectural best practices of our own for MongoDB in deployments on our platform.

The MongoDB engineered servers that you order from SoftLayer already implement several of the recommendations you'll see below, and I'll note which have been incorporated as we go through them. Given the scope of the topic, it's probably easiest to break down this guide into a few sections to make it a little more digestible. Installation. Getting documents from MongoDB collections with PHP. We have a MongoDB server running as a windows service on the same machine as our web server. Over time, this MongoDB server had been listening on port 33333 to help us remember information that we had collected about our friends.

As time passes, our memory can hardly rival that of our MongoDB server, which was designed to help us recollect information efficiently. Deploy Three Config Servers for Production Deployments — MongoDB Manual 2.4.3. Install MongoDB on Amazon EC2 — MongoDB Ecosystem 2.2.2. Storage Considerations EC2 instances can be configured either with ephemeral storage or persistent storage using the Elastic Block Store (EBS). Ephemeral storage is lost when instances are terminated so it is generally not recommended for use unless you’re comfortable with the data-loss implications. For almost all deployments EBS will be the better choice. Sharded Cluster Overview — MongoDB Manual 2.4.3. These documents present the details of sharding in MongoDB. These include the components, the architectures, and the behaviors of MongoDB sharded clusters. For an overview of sharding and sharded clusters, see Sharding Introduction.

Sharded Cluster Architectures — MongoDB Manual 2.4.3.