00:00:10.240
Hello everyone, I’m going to talk about Mongoid and some of the latest developments that have been happening. My name is Bernerd Schaefer, and you can find me on Twitter at @bjschaer, on GitHub, and my blog can be accessed at ut.com. I work for Hashrocket, and today we will discuss Mongoid and, in particular, how we have embraced Rails 3.
00:00:30.320
First, I want to wish a happy birthday to Mongoid as it’s been exactly one year since Duran first pushed the initial commit. Mongoid has been growing rapidly; we now have 88 contributors, with 40 of them contributing in the last two months alone. We’ve had 300 commits during that time, which highlights the impact and community behind Mongoid. We have excellent documentation, an active IRC channel, a mailing list, and our community is now global. We support multiple languages for error and validation messages, and we welcome new contributors who want to add more languages.
00:01:07.000
Today, we are particularly excited about Rails 3. We have really embraced it and are working hard to integrate it as much as possible in Mongoid. One notable feature is the rail ties, which allow you to quickly set up a new Mongoid app using a simple command. You can run 'rails new' and include the Mongoid gem, and it provides you with generators that help you create models just like you would with Active Record. Additionally, we have adopted Active Model for various validations that you would typically expect in Rails.
00:02:14.120
Serialization options like JSON and XML, among others, are seamlessly integrated into Mongoid, and mass assignment works similarly to Active Resource. However, it is still agnostic, allowing you to run Mongoid with Sinatra as well. All features related specifically to Rails are located within the rail tie, meaning they are only included if needed. Frameworks like Pedrino also have built-in support for Mongoid. Mongoid allows for the integration of other ORMs because, often, you need an SQL solution, and it supports Data Mapper and Active Record, making cross-associations easy.
00:02:50.760
Now, let's delve deeper into MongoDB. An object-document mapper (ODM) is the equivalent of an ORM, and it essentially interacts closely with its MongoDB database. I think it’s important to state that we have embraced all the core tenets of MongoDB. If there’s something you can do in MongoDB that you cannot do in Mongoid, that’s a bug, and we encourage you to file an issue. Mongoid is specifically built to support core database features like fast in-place updates, rich document-based querying, replication, and high availability.
00:03:56.760
For instance, let's discuss in-place updates. If you have a person document that embeds multiple addresses, the addresses are stored within the person document itself. Similarly, each address can embed multiple locations. For instance, if a person has a summer home, the locations might be city names. When you access the person’s attributes, you will see that the addresses attribute is an array of hashes that contain IDs along with an array of hashes for locations. With this structure, you can easily update nested attributes without having to push the entire document graph to the database.
00:05:16.200
Mongoid employs a more optimized approach by updating only what is necessary. When calling the update function on a nested document, it only pushes exactly what is changed, ensuring efficiency in data transfer. The framework tracks dirty changes and minimizes the amount of data sent across the wire, making for a more effective update mechanism. This efficiency applies to nearly all updates made within Mongoid.
00:06:36.840
Another great feature of MongoDB is its rich query language. This element distinguishes MongoDB from other document databases, such as CouchDB, which relies on a map-reduce methodology. MongoDB supports a query language that allows for complex queries while still maintaining the flexibility of a document database. You can construct typical queries that apply constraints like 'less than 24' or 'greater than 18,' and run those in parallel.
00:07:10.760
Furthermore, you can efficiently query nested documents. For example, if you want to find a person who has any address or location that matches 'Austin,' you can easily do that. This method works seamlessly with the active scope features in Mongoid as well. You can create scopes to bundle complex queries, allowing for organized and reusable code. This means that you can filter results by name using regular expressions or other conditions efficiently, all while keeping the query's execution performant.
00:08:43.560
Lastly, I want to touch on geospatial indexes, which were introduced in a recent Mongoid update. This feature allows for precise location-based queries. For example, you can input latitude and longitude values into an array to create a 2D GE index. Although 3D geospatial indexing isn’t yet available, it will be in the future, and if you're interested in this feature, please upvote the appropriate tickets. You can then use this geospatial information to find other locations nearby effectively.
00:09:43.440
Another important aspect to highlight is replication and high availability, which are fundamental features of NoSQL databases. In earlier MongoDB versions, replication followed a master-slave model, where you had one master and multiple slaves replicating from it. This system had its pitfalls, including potential lag in data synchronization, which could result in reading outdated information. To mitigate these challenges, Mongoid allows you to configure a list of slaves for read queries while writing to the master.
00:11:02.560
In the latest updates, however, we moved to using replica sets. This model enhances flexibility as there is no inherent master or slave. When starting a replica set, you initialize the server, configure it accordingly, and Mongoid will handle read-write operations effectively while ensuring data integrity across your nodes. You can set specific write concern settings to ensure that data is only returned once it has been successfully written to multiple nodes, providing more robust guarantees against data loss.
00:12:56.400
To support automatic failover, Mongoid’s Ruby driver monitors connections to the servers. If the primary server goes down, the driver automatically attempts to connect to an available secondary server, promoting it to primary status. This seamless transition means that data access continues with minimal interruption, enhancing the overall user experience. Recently, we have integrated many new features, and any updates introduced in MongoDB v6 will be supported in Mongoid within a short time span, typically just a week or so.
00:14:25.320
Looking ahead, the future of Mongoid will be driven by our commitment to provide support and enhancements based on community feedback and the evolving needs of developers. Our upcoming release candidate will be available soon, along with updates to enhance associations and ensure they function more efficiently. There are also several features under consideration, such as virtual collections for embedded documents, which would streamline the ability to query or insert data directly into embedded documents.
00:15:41.440
Improved date support has been a long-requested feature, and we are optimistic that it will be included soon. Other popular requests include capped arrays to manage embedded documents effectively. These features would allow Mongoid to become even more versatile in handling data, retaining performance while ensuring data consistency. So, if you have any questions, feel free to reach out, and I encourage everyone to contribute to the project – we love having new people get involved!