MongoDB CRUD operations
In this section we will cover CRUD operations using Ruby, Python, and PHP with the official MongoDB driver and some popular frameworks for each language respectively.
CRUD using the Ruby driver
In the previous chapter, we covered how to connect to MongoDB from Ruby, Python, and PHP using the drivers and ODM. In this chapter, we will explore create
, read
, update
, and delete
operations using the official drivers and the most commonly used Object Document Mapper (ODM) frameworks.
Creating documents
Using the process described in Chapter 2, Schema Design and Data Modeling, we assume that we have an @collection
instance variable pointing to our books
collection in a mongo_book
database in the 127.0.0.1:27017
default database:
@collection = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'mongo_book').database[:books]
Inserting a single document with our definition:
document = { isbn: '101', name: 'Mastering MongoDB', price: 30}
Can be done with a single line of code as...