Write Data to MongoDB
On this page
Overview
On this page, you can see copyable code examples that show common methods you can use to write data to MongoDB by using the Ruby driver.
Tip
To learn more about any of the methods shown on this page, see the link provided in each section.
To use an example from this page, copy the code example into the sample
application or your own application. Be sure to replace all
placeholders in the code examples, such as <connection string>
, with the
relevant values for your MongoDB deployment.
Sample Application
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have the Ruby driver installed in your Ruby project.
Copy the following code and paste it into a new
.rb
file.Copy a code example from this page and paste it on the specified lines in the file.
1 require 'bundler/inline' 2 gemfile do 3 source 'https://rubygems.org' 4 gem 'mongo' 5 end 6 7 uri = "<connection string>" 8 9 Mongo::Client.new(uri) do |client| 10 database = client.use('<database name>') 11 collection = database[:<collection name>] 12 13 # Start example code here 14 15 # End example code here 16 end
Insert One
The following code shows how to insert a single document into a collection:
document = { field_name: '<field value>' } collection.insert_one(document)
To learn more about the insert_one
method, see the
Insert Documents guide.
Insert Multiple
The following code shows how to insert multiple documents into a collection:
documents = [ { field_name: '<field value 1>' }, { field_name: '<field value 2>' } ] collection.insert_many(documents)
To learn more about the insert_many
method, see the
Insert Documents guide.
Update One
The following code shows how to update a single document in a collection by creating or editing a field:
filter = { field_name: '<field value>' } update = { <update definition> } collection.update_one(filter, update)
To learn more about the update_one
method, see the
Update Documents guide.
Update Multiple
The following code shows how to update multiple documents in a collection by creating or editing a field:
filter = { field_name: '<field value>' } update = { <update definition> } collection.update_many(filter, update)
To learn more about the update_many
method, see the
Update Documents guide.
Replace One
The following code shows how to replace a single document in a collection with a new document:
filter = { field_name: '<field value>' } new_document = { field_name: '<field value>' } collection.replace_one(filter, new_document)
To learn more about the replace_one
method, see the
Replace Documents guide.
Delete One
The following code shows how to delete a single document in a collection:
filter = { field_name: '<field value>' } collection.delete_one(filter)
To learn more about the delete_one
method, see the
Delete Documents guide.
Delete Multiple
The following code shows how to delete multiple documents in a collection:
filter = { field_name: '<field value>' } collection.delete_many(filter)
To learn more about the delete_many
method, see the
Delete Documents guide.
Bulk Write
The following code shows how to perform multiple write operations in a single bulk operation:
operations = [ { <bulk operation 1> }, { <bulk operation 2> }, { <bulk operation 3> }, ] collection.bulk_write(operations)
To learn more about the bulk_write
method, see the Bulk Write Operations guide.