Using MongoDB with Ruby: Using Documents

In the following subsections we shall add a document to MongoDB server, add a batch of documents, find a single document, find multiple documents, update documents, delete documents, and perform bulk operations.

1. Adding a Document

In this section we shall add a single document to a MongoDB collection. The insert_one(document, options = {}) method in the Collection class is used to add a document. The document parameter is the document to add. The options parameter is a customization Hash of options that defaults to {}.

  1. Create a Ruby script addDocument.rb and add require and include statements for mongo gem and Mongo namespace respectively.
  2. Create a connection, set database to local and get the mongodb collection.

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database => ‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

  1. The create() method must be invoked as the mongodb collection if it does not already exist.  collection.create
  2. Create a document JSON to add.

document1={

“_id” => “document1a”,

“catalogId” => “catalogl”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Engineering as a Service”,

“author” => “David A. Kelly”

}

  1. Invoke the insert_one() method to add the document to the mongodb collection.

collection.insert_one(document1)

  1. Similarly add another document.

document2={

“_id” => “document1a”,

“catalogId” => “catalog2”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Quintessential and Collaborative”,

“author” => “Tom Haunert”

}

collection.insert_one(document2)

The addDocument.rb script is listed:

require ‘mongo’

include Mongo

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database => ‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

collection.create document1={

“_id” => “document1a”,

“catalogId” => “catalog1”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Engineering as a Service”,

“author” => “David A. Kelly”

}

collection.insert_one(documentl)

document2={

“catalogId” => “catalog2”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Quintessential and Collaborative”,

“author” => “Tom Haunert”

}

collection.insert_one(document2)

  1. Run the addDocument.rb script with the following command.

>ruby addDocument.rb

The output from the addDocument.rb script is shown in Figure 4-14.

  1. Run the following JavaScript method in Mongo shell.

>db.mongodbfind()

The two documents added get listed as shown in Figure 4-15.

 

  1. The _ids of the documents added must be unique or the OperationFailure error gets generated. To demonstrate add two documents with the same _id. The Ruby script addDocument.rb to demonstrate OperationFailure error is listed:

require ‘mongo’

include Mongo

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database => ‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

collection.create

document1={

“_id” => “document1a”,

“catalogId” => “catalog1”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Engineering as a Service”,

“author” => “David A. Kelly”

}

collection.insert_one(document1)

document2={

“_id” => “document1a”,

“catalogId” => “catalog2”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Quintessential and Collaborative”,

“author” => “Tom Haunert”

}

When the script is run again with ruby addDocument.rb, the OperationFailure error gets generated as shown in Figure 4-16.

2. Adding Multiple Documents

In the preceding section we added two documents but by invoking the insert_one() method twice. The Mongo::Collection class provides the insert_many(documents, options = {}) method to add multiple documents.

  1. Create a Ruby script addDocuments.rb in the C:\Ruby21-x64\mongodbscripts directory.
  2. Create a Client instance and get the mongodb collection as before. So that the newly added documents do not have duplicate key with a document already in the mongodb collection, delete the mongodb collection before running the addDocuments.rb script using the db.mongodb.drop() method in the mongo shell.

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database => ‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

collection.create

  1. Create JSON for two documents.

document1={

“catalogId” => “catalog1”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Engineering as a Service”,

“author” => “David A. Kelly”

}

document2={

“catalogId” => “catalog2”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Quintessential and Collaborative”,

“author” => “Tom Haunert”

}

  1. Invoke the insert_many() method to add the two documents.

collection.insert_many([document1,document2])

The addDocuments.rb is listed:

require ‘mongo’

include Mongo

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database => ‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

collection.create

document1={

“catalogId” => “catalog1”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Engineering as a Service”,

“author” => “David A. Kelly”

}

document2={

“catalogId” => “catalog2”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Quintessential and Collaborative”,

“author” => “Tom Haunert”

}

Run the script with ruby addDocuments.rb. The output from the addDocuments.rb script is shown in Figure 4-17.

  1. To verify that the two documents got added, run the following command in Mongo shell.

>db.mongodb.find()

The two documents added get listed as shown in Figure 4-18.

In this section we shall also add multiple documents using a for loop.

  1. Drop the mongodb collection with db.mongodb.drop().
  2. Create another Ruby script addDocument2.rb and create the collection mongodb in the local database. First get the local database instance and subsequently invoke the create() method to create a collection.

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database => ‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

collection.create

  1. Using a for loop, create a variable for catalogId and create a new document instance using the catalogId variable value in each iteration of the for loop. Add the document instance to the collection using the insert_one() method. The addDocument2.rb script is listed:

require ‘mongo’

include Mongo

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database => ‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

collection.create

for i in 1..10 do

catalogId=”catalog”+i.to_s

document={

“catalogId” => catalogId,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”

}

collection.insert_one(document)

print “\n”

end

  1. Run the addDocument2.rb script with the following command.

>ruby addDocument2.rb

Multiple documents get added as shown by the output from the script in Figure 4-19.

5. Subsequently run the following JavaScript method in Mongo shell to list the documents added.

>db.mongodb.find()

The documents added get listed as shown in Figure 4-20.

3. Finding a Single Document

The find(filter = nil) method in Mongo::Collection class is used to find one or more documents.

By default, if no filter is specified, all documents get found. In this section we shall find a single document using a filter.

  1. Create a Ruby script findDocument.rb in the C:\Ruby21-x64\mongodbscripts directory.
  2. Create the mongodb collection as before.
  3. Add two documents using the insert_many() method as in the addDocuments.rb script.
  4. Invoke the find() method with a filter specifying catalogId as catalog!.. Iterate over the result cursor to output the documents found.

collection.find(:catalogId=>”catalog1″).each do |document| print document end

The findDocument.rb script is listed below.

require ‘mongo’

include Mongo

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database => ‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

collection.create

document1={

“catalogId” => “catalogl”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Engineering as a Service”,

“author” => “David A. Kelly”

}

document2={

“catalogId” => “catalog2”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Quintessential and Collaborative”,

“author” => “Tom Haunert”

} collection.insert_many([document1,document2])

collection.find(:catalogId=>”catalog1″).each do |document|0 print document end

  1. Drop the mongodb collection with db.mongodb.drop(). Run the script with the following command.

>ruby findDocument.rb

The document with catalogId catalog! gets found and output as shown in Figure 4-21.

4. Finding Multiple Documents

In this section we shall find multiple documents using the find() method. The find(filter = nil) method may be used to find multiple documents that match the filter. The find() method returns a cursor. If the find() method invocation includes a block, as with invoking the each() method on the result of invoking the find() method, the method yields a cursor to the block that may be iterated over to output the documents.

  1. To find documents using the find() method create a Ruby script findDocuments.rb in the C:\Ruby21-x64\mongodbscripts directory.
  2. Create a collection instance as discussed before. The find() method by itself does not return a Cursor object but returns a CollectionView that creates a Cursor. Each of the following returns a CollectionView object.

print collection.find()

print collection.find({“journal” => “Oracle Magazine”})

  1. The Cursor class implements the Enumerable, which implies that the Enumerable methods such as each may be used. To output the documents in the Cursor, iterate over the result set using the each method.

collection.find.each { |document| puts document }

  1. To output all documents use the Enumerable#find.to_a() method, which gets all documents into the memory at once thus making the method inefficient in comparison to the each() method with which one document at a time in is processed in the block iteration.

puts collection.find.to_a

  1. In the findDocuments.rb script create an array of documents and add using the insert_many script.

collection.insert_many([document1,document2])

  1. Try each of the following options for finding documents:
  • Using the find() method find all documents and invoke each over the Cursor generated by the CollectionView to iterate over the collection of documents returned.

collection.find().each do |document|

print document

end

  • As another example find only documents with journal set to ‘Oracle Magazine’ and limit the number of documents returned using the limit() method.

collection.find(:journal => ‘Oracle Magazine’).limit(5).each do |document|

print document

end

  • To find the number of documents for a particular edition use the count() method.
    print collection.find(:edition => ‘November December 2013’).count
  • Distinct documents may be found using the distinct() method.

print collection.find.distinct(:catalogId)

collection.find(:journal => ‘Oracle Magazine’).limit(5).each do |document|

print document

end

The findDocuments.rb script is listed below.

require ‘mongo’ include Mongo

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database => ‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

collection.create

document1={

“catalogId” => “catalog1”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Engineering as a Service”,

“author” => “David A. Kelly”

}

document2={

“catalogId” => “catalog2”,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Quintessential and Collaborative”,

“author” => “Tom Haunert”

}

collection.insert_many([document1,document2])

collection.find().each do |document|

print document

end

collection.find(:journal => ‘Oracle Magazine’).limit(5).each do |document|

print document

end

print “Number of documents with edition November December 2013: “

print “\n”

print collection.find(:edition => ‘November December 2013’).count

print “\n”

print “Number of distinct documents by catalogId: ”

print “\n”

print collection.find.distinct(:catalogId)

  1. Drop the mongodb collection with db.mongodb.drop() as the collection is used again. Run the findDocuments.rb script with the following command.

>ruby findDocuments.rb

The output from the findDocuments.rb script is shown in Figure 4-22.

The output from collection.find() lists the two documents in the mongodb collection. The output from collection.find(:journal => ‘Oracle Magazine’).limit(5) outputs documents with journal set to ‘Oracle Magazine’. The limit() method limits the result to 5, but because the collection has only 2 documents the limit method does not have an effect. The collection.find(:edition => ‘November December 2013’).count() method invocation outputs the document count for documents with edition as ‘November December 2013’ as 2. The collection.find.distinct(:catalogId) method finds the 2 distinct catalogIds in the mongodb collection.

The find() method returns a collection view and when each is invoked on the view a Cursor object is created. The Mongo::Collection::View is a representation of a query and options generating a result set of documents. The find() method by itself does not send the query to the server. Another method has to be invoked subsequent to the find() method to send the query to the server. For example, when the each() method is invoked on a View a Cursor object is created which sends the query to the server. The Mongo::Collection::View class supports the methods discussed in Table 4-10. These methods are included from Writable, Explainable, Readable, and Iterable classes.

5. Updating Documents

The Collection class does not directly provide any instance methods for updating documents. But the collection view returned by the find() method provides several methods as discussed in Table 4-10 in the preceding section to update or replace document/s. In this section we shall update documents.

  1. Create a Ruby script updateDocument.rb in the C:\Ruby21-x64\mongodbscripts directory.
  2. Delete the mongodb collection if already present and create the mongodb collection in the updateDocument.rb script.
  3. Add two documents to the mongodb collection.
  4. Create a collection view for the mongodb collection.

collection = client[:mongodb]

We shall discuss several examples for which the two documents shall be added to the mongodb collection and subsequently updated or replaced.

Example 1

  1. For the first example, use the update_one() method to increment the catalogId of one of the documents by 1. The $inc update operator is used to increment the catalogId. The find() method finds all documents with journal set to ‘Oracle Magazine’.

result = collection.find(:journal =>

‘Oracle Magazine’).update_one(“$inc” => { :catalogId => 1 })

  1. Output the number of documents updated. print result.n
  2. Run the updateDocument.rb script.

>ruby updateDocument.rb

As the output shown in Figure 4-23 shows, one document gets updated.

  1. Run the db.mongodb.find() command in Mongo shell to list the updated document as shown in Figure 4-24. Both documents have catalogId as 2; one document had catalogId 1 to start with and the other catalogId has been incremented by 1.

Example 2

  1. For the second example use the update_many method to update multiple documents. Use the $set update operator to set publisher field to OraclePublishing. The find() method has a filter set to find all documents with publisher set to ‘Oracle Publishing’.

result = collection.find(:publisher => ‘Oracle Publishing’).update_ many

(‘$set’ => { publisher: ‘OraclePublishing’})

  1. Drop the mongodb collection with db.mongodb.drop(). Run the updateDocument.rb script. The output indicates that two documents have been updated as shown in Figure 4-25.

  1. Run the db.mongodb.find() method to list the two updated documents as shown in Figure 4-26.

Example 3

  1. In the third example find the documents with edition set to November December 2013 and use the replace_one() method to replace one document with a document with only an edition field set to ’11-12-2013′.

result = collection.find(:edition => ‘November December 2013’).

replace_one(:edition => ’11-12-2013′)

  1. Drop the mongodb collection with db.mongodb.drop(). Run the updateDocument.rb script with just Example 3. As the output indicates one document has been updated (replaced) as shown in Figure 4-27.

  1. Run the db.mongodb.find() command in Mongo shell to list the replacement document as shown in Figure 4-28.

Example 4

  1. In the fourth example the find() method filter is set to find all documents with journal set to ‘Oracle Magazine’ and the find_one_and_replace() method is used to find and replace one document with a document with just the journal field set. The return_document is set to :after to return the document after replacement.

document = collection.find(:journal => ‘Oracle Magazine’).find_one_ and_replace

({:journal => ‘OracleMagazine’}, :return_document => :after)

Drop the mongodb collection with db.mongodb.drop(). When the updateDocument.rb script is run, one document gets replaced and the replaced document gets output as shown in Figure 4-29.

  1. Drop the mongodb collection with db.mongodb.drop(). Run the db.mongodb.find() command in Mongo shell to list the documents including the replaced document as shown in Figure 4-30.

Example 5

In the fifth example the find() method filter is set to find all documents with edition set to ‘November December 2013’ and the find_one_and_update() method is used to find and update the edition field using the $set operator.

document = collection.find(:edition => ‘November December 2013’).find_one_and_

update(‘$set’ => {:edition=>’11-12-2013′})

When the updateDocument.rb script is run, one of the documents with edition set to ‘November December 2013’ gets updated and the document before the update gets output, which is the default if :return_document is not set to :after, as shown in Figure 4-31.

When the db.mongodb.find() command is run in Mongo shell the updated document gets listed as shown in Figure 4-32

The updateDocument.rb script is listed with each of the five examples commented out. Run the script by uncommenting the example code to run.

require ‘mongo’

include Mongo

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database => ‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

collection.create

document1={

“catalogId” => 1,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Engineering as a Service”,

“author” => “David A. Kelly”

}

document2={

“catalogId” => 2,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Quintessential and Collaborative”,

“author” => “Tom Haunert”

}

collection.insert_many([document1,document2])

print “\n”

collection = client[:mongodb]

#Update example 1

#result = collection.find(:journal => ‘Oracle Magazine’).update_one(“$inc” => { :catalogId => 1 })

#print “Number of documents updated: “

#print “\n”

#print result.n #print “\n”

#Update example 2

#result = collection.find(:publisher => ‘Oracle Publishing’).update_many(‘$set’ =>

{ publisher: ‘OraclePublishing’})

#print “Number of documents updated: “

#print “\n”

#print result.n

#print “\n”

#Update example 3

#result = collection.find(:edition => ‘November December 2013′).replace_one(:edition => ’11­12-2013’)

#print “Number of documents updated: “

#print “\n”

#print result.n

#print “\n”

#Update example 4

#document = collection.find(:journal => ‘Oracle Magazine’).find_one_and_replace

({:journal => ‘OracleMagazine’}, :return_document => :after)

#print “Document after being updated: “

#print “\n”

#print document

#print “\n”

#Update example 5

#document = collection.find(:edition => ‘November December 2013’).find_one_and_update

(‘$set’ => {:edition=>’11-12-2013′})

#print “Document before being updated: “

#print “\n”

#print document

#print “\n”

6. Deleting Documents

In this section we shall delete documents from a MongoDB collection. Methods for deleting document/s are included in Table 4-10 (see the “Finding Multiple Documents” section).

  1. Create a Ruby script deleteDocument.rb and create a Collection instance for the mongodb collection.
  2. Add four documents to the collection using the insert_many() method.

collection.create

document1={

“catalogId’1 => 1,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Engineering as a Service”,

“author” => “David A. Kelly”

}

document2={

“catalogId” => 2,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Quintessential and Collaborative”,

“author” => “Tom Haunert”

}

document3={

“catalogId” => 3,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “”,

“author” =>   “”

}

document4={

“catalogId” => 4,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “”,

“author” =>   “”

}

collection.insert_many([document1,document2,document3,document4])

  1. Create a Collection instance over the mongodb collection. collection = client[:mongodb]
  2. Subsequently remove one of the documents using the delete_one() method.

The find() method filter is set to find all documents with edition set to ‘November December 2013’.

print collection.find(:edition => ‘November December 2013’).delete_one

As another example, delete a document using find_one_and_delete() method.

print collection.find(:journal => ‘Oracle Magazine’).find_one_and_delete

As a third example, delete multiple documents using the delete_many() method with the find() method filter set to find all documents with edition set to ’November December 2013’.

print collection.find(:edition => ‘November December 2013’).delete_many

  1. The deleteDocument.rb script is listed below with some of the code commented out. First, uncomment the first two examples of deleting and run the script.

Subsequently delete the mongodb collection and run the script with the third example to delete all documents.

require ‘mongo’

include Mongo

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database =>

‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

#collection.create

document1={

“catalogId” => 1,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Engineering as a Service”,

“author” => “David A. Kelly”

}

document2={

“catalogId” => 2,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Quintessential and Collaborative”,

“author” => “Tom Haunert”

}

document3={

“catalogId” => 3,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “”,

“author” =>  “”

}

document4={

“catalogId” => 4,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “”,

“author” =>         “”

}

collection.insert_many([document1,document2,document3,document4])

print “\n”

collection = client[:mongodb]

#print collection.find(:edition => ‘November December 2013’).delete_one

print “\n”

#print collection.find(:journal => ‘Oracle Magazine’).find_one_and_delete

print “\n”

#print collection.find(:edition => ‘November December 2013’).delete_many

print “\n”

  1. Drop the mongodb collection with db.mongodb.drop(). Uncomment the delete_one and find_one_and_delete_one and run the deleteDocument.rb script to delete two of the four documents, one each with delete_one and find_one_and_delete.

The output from the script is shown in Figure 4-33.

 

  1. Run the db.mongodb.find() command in Mongo shell to list two of the remaining documents as shown in Figure 4-34.

  1. Delete the mongodb collection with the db.mongodb.drop() command in Mongo shell and subsequently run the deleteDocument.rb script again with just the third example of delete_many. The output from the deleteDocument.rb script is shown in Figure 4-35.

When the db.mongodb.find() command is run in Mongo shell no document gets listed, as shown in Figure 4-36.

7. Performing Bulk Operations

The MongoDB Ruby driver supports bulk operations using the bulk_write(operations, options) method in the Mongo::Collection class. A list of operations may be invoked using the bulk_write() method. Each operation is defined with a document with one of the keys discussed in Table 4-11.

  1. Create a Ruby script bulk.rb in the C:\Ruby21-x64\mongodbscripts directory.
  2. Drop the mongodb collection if already present with db.mongodb.drop() and create the collection in the script.
  3. Insert multiple documents using the insert_many() method. collection.insert_many([document1,document2,document3,document4])
  4. Create a collection for the mongodb collection. collection = client[:mongodb]
  5. Invoke the bulk_write() method with bulk operations insert_one,update_one, and replace_one. Set the second argument to :ordered => true to indicate that the bulk operations are to be performed in the specified order, which is also the default.

The bulk.rb script is listed below.

require ‘mongo’

include Mongo

client =Mongo::Client.new([ ‘127.0.0.1:27017’ ], :database => ‘test’)

client=client.use(:local)

db=client.database

collection=db.collection(“mongodb”)

collection.create

document1={

“catalogId” => 1,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Engineering as a Service”,

“author” => “David A. Kelly”

}

document2={

“catalogId” => 2,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “Quintessential and Collaborative”,

“author” => “Tom Haunert”

}

document3={

“catalogId” => 3,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “”,

“author” =>       “”

}

document4={

“catalogId” => 4,

“journal” => “Oracle Magazine”,

“publisher” => “Oracle Publishing”,

“edition” => “November December 2013”,

“title” => “”,

“author” =>       “”

}

collection.insert_many([document1,document2,document3,document4])

print “\n”

collection = client[:mongodb]

collection.bulk_write([ { :insert_one => { :catalogId => 5 }

},

{ :update_one => { :find => { :catalogId => 1 },

:update => {‘$set’ => { :catalogId => 6 } }

  }

},

{ :replace_one => { find => { :catalogId => 2 },

  replacement => { :catalogId => 7 }

   }

}

],

:ordered => true )

  1. Run the bulk.rb script as follows.

>ruby bulk.rb

The output from the bulk.rb script is shown in Figure 4-37.

7. Subsequently list the documents in the mongodb collection using the db.mongodb.find() method. As shown in Figure 4-38, a new document with catalogId as 5 has been added. The catalogId 1 has been updated to 6 and the document with catalogId 2 has been replaced with a document with catalogId 7.

Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *