Deleting a collection using MongoDB Ruby Driver

Suppose there is a db named ‘music’ with a collection ‘rock_bands’, deleting this collection from this db using the MongoDB ruby driver would be like this :-

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

Using Mongo Shell here’s how you drop a collection :-

> use music

switched to db music

> db.rock_bands.drop()

true

Advertisements

Querying using mongo ruby driver

Suppose you have a db named ‘bands’ with a collection named ‘metal’, finding the band named ‘Dream Theater’ would be like this :-

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'bands')

client[:metal].find(:name => 'Dream Theater').each do |doc|
  #=> Yields a BSON::Document.
puts doc
end
You can then convert the bson document to json by using ‘to_json’ and proceed with manipulation of the data