-
Notifications
You must be signed in to change notification settings - Fork 28
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
lindseymoore
merged 9 commits into
mongodb:standardization
from
lindseymoore:DOCSP-45202
Jan 16, 2025
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e8badd1
DOCSP-45202 Index Overview
lindseymoore 38ba81a
edits
lindseymoore dbe21b3
fix ref
lindseymoore 9116c92
atlas search updates
lindseymoore a6c714d
small fix
lindseymoore a18ad37
add api section, edit atlas search examples
lindseymoore 86c6189
edits
lindseymoore bdbf28e
Merge branch 'standardization' of github.com:mongodb/docs-ruby into D…
lindseymoore e10fed3
tech review
lindseymoore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }} | ||||||
|
||||||
# Creates a new client and connect to the server | ||||||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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' } ) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
Our team coding style prefers single quoted strings, except for cases where you need string interpolation, or if the string itself contains single quotes:
When specifying the server API version, an integer will automatically be converted to a string.
There was a problem hiding this comment.
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.