Skip to content

Commit 6d52f88

Browse files
authored
Merge branch 'standardization' into DOCSP-45205
2 parents 48e9326 + 96c104d commit 6d52f88

21 files changed

+2047
-9
lines changed

source/includes/indexes/compound.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'mongo'
2+
3+
# Replace the placeholders with your credentials
4+
uri = "<connection string>"
5+
6+
# Sets the server_api field of the options object to Stable API version 1
7+
options = { server_api: { version: "1" }}
8+
9+
# Creates a new client and connect to the server
10+
client = Mongo::Client.new(uri, options)
11+
12+
# start-sample-data
13+
database = client.use('sample_mflix')
14+
collection = database[:movies]
15+
# end-sample-data
16+
17+
# Creates an index on the "runtime" and "year" field
18+
# start-index-compound
19+
collection.indexes.create_one({ runtime: -1, year: 1 })
20+
# end-index-compound
21+
22+
# Finds a document with the specified runtime and release year by using the newly created index
23+
# start-index-compound-query
24+
filter = { '$and' => [
25+
{ runtime: { '$gt' => 90 } },
26+
{ year: { '$gt' => 2005 } }
27+
] }
28+
doc = collection.find(filter).first
29+
30+
if doc
31+
puts doc.to_json
32+
else
33+
puts "No document found"
34+
end
35+
# end-index-compound-query
36+
37+
# Lists all indexes on the collection
38+
# start-check-compound-index
39+
puts collection.indexes.collect(&:to_json)
40+
# end-check-compound-index

source/includes/read/count.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'bundler/inline'
2+
3+
gemfile do
4+
source 'https://rubygems.org'
5+
gem 'mongo'
6+
end
7+
8+
uri = '<connection string>'
9+
10+
Mongo::Client.new(uri) do |client|
11+
# start-db-coll
12+
database = client.use('sample_training')
13+
collection = database['companies']
14+
# end-db-coll
15+
16+
# Counts all documents in the collection
17+
# start-count-all
18+
result = collection.count_documents
19+
puts "Number of documents: #{result}"
20+
# end-count-all
21+
22+
# Counts documents that have a "founded_year" value of 2010
23+
# start-count-accurate
24+
result = collection.count_documents(founded_year: 2010)
25+
puts "Number of companies founded in 2010: #{result}"
26+
# end-count-accurate
27+
28+
# Counts a maximum of 100 documents that have a "number_of_employees" value of 50
29+
# start-modify-accurate
30+
result = collection.count_documents({ number_of_employees: 50 }, limit: 100)
31+
puts "Number of companies with 50 employees: #{result}"
32+
# end-modify-accurate
33+
34+
# Estimates the number of documents in the collection
35+
# start-count-estimate
36+
result = collection.estimated_document_count
37+
puts "Estimated number of documents: #{result}"
38+
# end-count-estimate
39+
40+
# Estimates the number of documents in the collection and sets a time limit on the operation
41+
# start-modify-estimate
42+
result = collection.estimated_document_count(max_time_ms: 1000)
43+
puts "Estimated number of documents: #{result}"
44+
# end-modify-estimate
45+
end

source/includes/read/distinct.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'bundler/inline'
2+
3+
gemfile do
4+
source 'https://rubygems.org'
5+
gem 'mongo'
6+
end
7+
8+
uri = '<connection string>'
9+
10+
Mongo::Client.new(uri) do |client|
11+
# Access the database and collection
12+
# start-db-coll
13+
database = client.use('sample_restaurants')
14+
collection = database[:restaurants]
15+
# end-db-coll
16+
17+
# Retrieves distinct values of the "borough" field
18+
# start-distinct
19+
results = collection.distinct('borough')
20+
results.each do |value|
21+
puts value
22+
end
23+
# end-distinct
24+
25+
# Retrieves distinct "borough" field values for documents with a "cuisine" value of "Italian"
26+
# start-distinct-with-query
27+
results = collection.distinct('borough', { cuisine: 'Italian' })
28+
results.each do |value|
29+
puts value
30+
end
31+
# end-distinct-with-query
32+
33+
# Retrieves distinct "name" field values for documents matching the "borough" and "cuisine" fields query
34+
# and uses primary preferred read preference
35+
# start-distinct-with-opts
36+
filter = { borough: 'Bronx', cuisine: 'Pizza' }
37+
options = { read: { mode: :primary_preferred } }
38+
results = collection.distinct('name', filter, options)
39+
40+
results.each do |value|
41+
puts value
42+
end
43+
# end-distinct-with-opts
44+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'bundler/inline'
2+
3+
gemfile do
4+
source 'https://rubygems.org'
5+
gem 'mongo'
6+
end
7+
8+
uri = '<connection string>'
9+
10+
Mongo::Client.new(uri) do |client|
11+
# Access the database and collection
12+
# start-db-coll
13+
database = client.use('sample_restaurants')
14+
collection = database[:restaurants]
15+
# end-db-coll
16+
17+
# Retrieves 5 documents that have a "cuisine" value of "Italian"
18+
# start-limit
19+
filter = { cuisine: 'Italian' }
20+
collection.find(filter)
21+
.limit(5)
22+
.each { |doc| puts doc }
23+
# end-limit
24+
25+
# Retrieves documents with a "cuisine" value of "Italian" and sorts in ascending "name" order
26+
# start-sort
27+
filter = { cuisine: 'Italian' }
28+
collection.find(filter)
29+
.sort(name: 1)
30+
.each { |doc| puts doc }
31+
# end-sort
32+
33+
# Retrieves documents with a "borough" value of "Manhattan" but skips the first 10 results
34+
# start-skip
35+
filter = { borough: 'Manhattan' }
36+
collection.find(filter)
37+
.skip(10)
38+
.each { |doc| puts doc }
39+
# end-skip
40+
41+
# Retrieves 5 documents with a "cuisine" value of "Italian", skips the first 10 results,
42+
# and sorts by ascending "name" order
43+
# start-limit-sort-skip
44+
filter = { cuisine: 'Italian' }
45+
collection.find(filter)
46+
.limit(5)
47+
.skip(10)
48+
.sort(name: 1)
49+
.each { |doc| puts doc }
50+
# end-limit-sort-skip
51+
end

source/includes/read/project.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'bundler/inline'
2+
3+
gemfile do
4+
source 'https://rubygems.org'
5+
gem 'mongo'
6+
end
7+
8+
uri = '<connection string>'
9+
10+
Mongo::Client.new(uri) do |client|
11+
# Access database and collection
12+
# start-db-coll
13+
database = client.use('sample_restaurants')
14+
collection = database[:restaurants]
15+
# end-db-coll
16+
17+
# Retrieves documents matching the "name" field query
18+
# and projects their "name", "cuisine", and "borough" values
19+
# start-project-include
20+
opts = { projection: { name: 1, cuisine: 1, borough: 1 } }
21+
collection.find({ name: 'Emerald Pub' }, opts).each do |doc|
22+
puts doc
23+
end
24+
# end-project-include
25+
26+
# Retrieves documents matching the "name" field query
27+
# and projects their "name", "cuisine", and "borough" values while excluding the "_id" values
28+
# start-project-include-without-id
29+
opts = { projection: { name: 1, cuisine: 1, borough: 1, _id: 0 } }
30+
collection.find({ name: 'Emerald Pub' }, opts).each do |doc|
31+
puts doc
32+
end
33+
# end-project-include-without-id
34+
35+
# Retrieves documents matching the "name" field query
36+
# and excludes their "grades" and "address" values when printing
37+
# start-project-exclude
38+
opts = { projection: { grades: 0, address: 0 } }
39+
collection.find({ name: 'Emerald Pub' }, opts).each do |doc|
40+
puts doc
41+
end
42+
# end-project-exclude
43+
end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
require 'bundler/inline'
2+
3+
gemfile do
4+
source 'https://rubygems.org'
5+
gem 'mongo'
6+
end
7+
8+
uri = '<connection string>'
9+
10+
Mongo::Client.new(uri) do |client|
11+
# start-setup
12+
database = client.use('db')
13+
collection = database[:fruits]
14+
15+
# Inserts documents representing fruits
16+
fruits = [
17+
{ _id: 1, name: 'apples', qty: 5, rating: 3, color: 'red', type: ['fuji', 'honeycrisp'] },
18+
{ _id: 2, name: 'bananas', qty: 7, rating: 4, color: 'yellow', type: ['cavendish'] },
19+
{ _id: 3, name: 'oranges', qty: 6, rating: 2, type: ['naval', 'mandarin'] },
20+
{ _id: 4, name: 'pineapples', qty: 3, rating: 5, color: 'yellow' }
21+
]
22+
23+
collection.insert_many(fruits)
24+
# end-setup
25+
26+
# Retrieves documents in which the "color" value is "yellow"
27+
# start-find-exact
28+
filter = { color: 'yellow' }
29+
results = collection.find(filter)
30+
results.each do |doc|
31+
puts doc
32+
end
33+
# end-find-exact
34+
35+
# Retrieves and prints documents in which the "rating" value is greater than 2
36+
# start-find-comparison
37+
filter = { rating: { '$gt' => 2 } }
38+
results = collection.find(filter)
39+
results.each do |doc|
40+
puts doc
41+
end
42+
# end-find-comparison
43+
44+
# Retrieves and prints documents that match one or both query filters
45+
# start-find-logical
46+
filter = { '$or' => [{ qty: { '$gt' => 5 } }, { color: 'yellow' }] }
47+
results = collection.find(filter)
48+
results.each do |doc|
49+
puts doc
50+
end
51+
# end-find-logical
52+
53+
# Retrieves and prints documents in which the "type" array has 2 elements
54+
# start-find-array
55+
filter = { type: { '$size' => 2 } }
56+
results = collection.find(filter)
57+
results.each do |doc|
58+
puts doc
59+
end
60+
# end-find-array
61+
62+
# Retrieves and prints documents that have a "color" field
63+
# start-find-element
64+
filter = { color: { '$exists' => true } }
65+
results = collection.find(filter)
66+
results.each do |doc|
67+
puts doc
68+
end
69+
# end-find-element
70+
71+
# Retrieves and prints documents in which the "name" value has at least two consecutive "p" characters
72+
# start-find-evaluation
73+
filter = { name: /p{2,}/ }
74+
results = collection.find(filter)
75+
results.each do |doc|
76+
puts doc
77+
end
78+
# end-find-evaluation
79+
end

source/includes/write/delete.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'bundler/inline'
2+
gemfile do
3+
source 'https://rubygems.org'
4+
gem 'mongo'
5+
end
6+
7+
uri = "<connection string URI>"
8+
9+
Mongo::Client.new(uri) do |client|
10+
# start-db-coll
11+
database = client.use('sample_restaurants')
12+
collection = database[:restaurants]
13+
# end-db-coll
14+
15+
# start-delete-one
16+
filter = { name: 'Happy Garden' }
17+
result = collection.delete_one(filter)
18+
puts "Deleted #{result.deleted_count} document(s)"
19+
# end-delete-one
20+
21+
# start-delete-many
22+
filter = { name: 'Starbucks', borough: 'Brooklyn' }
23+
result = collection.delete_many(filter)
24+
puts "Deleted #{result.deleted_count} document(s)"
25+
# end-delete-many
26+
27+
# start-delete-options
28+
filter = { name: /Red/ }
29+
options = { hint: 'name_index' }
30+
result = collection.delete_many(filter, options)
31+
puts "Deleted #{result.deleted_count} document(s)"
32+
# end-delete-options
33+
end

source/includes/write/replace.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'bundler/inline'
2+
gemfile do
3+
source 'https://rubygems.org'
4+
gem 'mongo'
5+
end
6+
7+
uri = "<connection string URI>"
8+
9+
Mongo::Client.new(uri) do |client|
10+
# start-db-coll
11+
database = client.use('sample_restaurants')
12+
collection = database[:restaurants]
13+
# end-db-coll
14+
15+
# Replaces a single document in the collection
16+
# start-replace-one
17+
filter = { name: 'Primola Restaurant' }
18+
new_document = { name: 'Frutti Di Mare', cuisine: 'Seafood', borough: 'Queens' }
19+
result = collection.replace_one(filter, new_document)
20+
puts "Replaced #{result.modified_count} document(s)"
21+
# end-replace-one
22+
23+
# Uses the upsert option to replace a single document in the collection
24+
# start-replace-options
25+
options = { upsert: true }
26+
result = collection.replace_one(filter, new_document, options)
27+
puts "Replaced #{result.upserted_count} document(s)"
28+
# end-replace-options
29+
end

0 commit comments

Comments
 (0)