Skip to content

DOCSP-45202 Index Overview #112

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 9 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions source/includes/indexes/index-code-examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
require 'mongo'

# Replace the placeholders with your credentials
uri = "<connection string>"

# Sets the server_api field of the options object to Stable API version 1
options = { server_api: { version: "1" }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] I noticed the other placeholders in this doc use single quotes, do these need to be in double quotes? I'm actually not sure the preference in Ruby but curious if they should all be single quotes for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the correct syntax is. It seems to work with single quotes, double quotes, and as a plain integer! Will ask tech reviewer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruby allows string literals to be specified with either single quotes or double quotes. Single quoted strings, however, will not interpolate expressions embedded in them. For example:

age = 21
puts "My age is #{age}" # -> prints "My age is 21"
puts 'My age is #{age}' # -> prints "My age is #{age}"

Our team coding style prefers single quoted strings, except for cases where you need string interpolation, or if the string itself contains single quotes:

"Hello world"        # ❌ 
'Hello world'        # ✅ 
'My name is #{name}' # ❌ 
"My name is #{name}" # ✅ 
'It\'s a string'     # ❌ 
"It's a string"      # ✅ 

# etc.

When specifying the server API version, an integer will automatically be converted to a string.

Copy link
Contributor Author

@lindseymoore lindseymoore Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for explanation! Just checked how we stated the version type on the Stable API page, and we used single quotes. For consistency and since it gets passed as a string anyway, I'll stick with single quotes.


# Creates a new client and connect to the server
client = Mongo::Client.new(uri, options)

database = client.use('<database name>')
collection = database[:<collection name>]

# Single Field
# start-index-single
collection.indexes.create_one({ <field name>: 1 })
# end-index-single

# Compound
# start-index-compound
collection.indexes.create_one({ <field name 1>: -1, <field name 2>: 1 })
# end-index-compound

# Multikey
# start-index-multikey
collection.indexes.create_one({ <field name>: 1 })
# end-index-multikey

# Geospatial
# start-index-geospatial
collection.indexes.create_one({ <GeoJSON field name>: '2dsphere' })
# end-index-geospatial

# Atlas Search

# Create Search Index
# start-create-search-index
index_definition = {
name: '<index name>',
definition: {
mappings: {
dynamic: false,
fields: { <field name>: {type: '<field type>'} }
}
}
}
collection.database.command(
createSearchIndexes: '<collection name>',
indexes: [index_definition]
)
# end-create-search-index

# List Search Indexes
# start-list-search-indexes
collection.database.command(listSearchIndexes: '<collection name>')
# end-list-search-indexes

# Update Search Indexes
#start-update-search-indexes
updated_definition = {
mappings: {
dynamic: false,
fields: { <updated field name>: { type: '<field type>' } }
}
}
collection.database.command(
updateSearchIndex: '<collection name>',
name: '<index name>',
definition: updated_definition
)
#end-update-search-indexes

# Delete Search Index
# start-drop-search-index
collection.database.command(
dropSearchIndex: '<collection name>',
name: '<index name>'
)
# end-drop-search-index

# Text Index
# start-text
collection.indexes.create_one( { :<field name> => 'text' } )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
collection.indexes.create_one( { :<field name> => 'text' } )
collection.indexes.create_one( { <field name>: 'text' } )

# end-text

# Create Many
# start-index-create-many
collection.indexes.create_many([
{ key: { <field name 1>: 1 } },
{ key: { <field name 2>: -1 } },
])
# end-index-create-many

# Delete an Index
# start-drop-single-index
collection.indexes.drop_one( '<index name>' )
# end-drop-single-index

# Drops all indexes in the collection.
# start-drop-all-index
collection.indexes.drop_all
# end-drop-all-index

# List an Index
# start-list-indexes
all_indexes.each do |index|
puts index
end
# end-list-indexes

client.close
19 changes: 19 additions & 0 deletions source/includes/indexes/index-starter-code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'mongo'

# Replace the placeholders with your credentials
uri = "<connection string>"

# Sets the server_api field of the options object to Stable API version 1
options = { server_api: { version: "1" }}

# Creates a new client and connect to the server
client = Mongo::Client.new(uri, options)

database = client.use('<database name>')
collection = database[:<collection name>]

# Start example code here

# End example code here

client.close
Loading
Loading