Skip to content

Commit a18e014

Browse files
committed
Merge remote-tracking branch 'upstream/master' into DOCSP-46818-client-bulk-write
2 parents 51a35d1 + 0b2a4ba commit a18e014

File tree

7 files changed

+307
-17
lines changed

7 files changed

+307
-17
lines changed

config/redirects

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
define: prefix docs/drivers/go
22
define: base https://www.mongodb.com/${prefix}
3-
define: versions v1.7 v1.8 v1.9 v1.10 v1.11 v1.12 v1.13 v1.14 v1.15 v1.16 v1.17 v2.0 master
3+
define: versions v1.7 v1.8 v1.9 v1.10 v1.11 v1.12 v1.13 v1.14 v1.15 v1.16 v1.17 v2.0 v2.1 master
44

55
symlink: current -> master
66

snooty.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ api-version = "v2"
1818
driver-long = "MongoDB Go Driver"
1919
driver-short = "Go driver"
2020
docs-branch = "master"
21-
version = "v2.0"
22-
full-version = "{+version+}.1"
21+
version = "v2.1"
22+
full-version = "{+version+}.0"
2323
example = "https://raw.githubusercontent.com/mongodb/docs-golang/{+docs-branch+}/source/includes/usage-examples/code-snippets"
2424
api = "https://pkg.go.dev/go.mongodb.org/mongo-driver/{+api-version+}"
2525
stable-api = "Stable API"
@@ -29,5 +29,5 @@ kms-long = "Key Management System"
2929
cmk-long = "Customer Master Key"
3030
dek-long = "Data Encryption Key"
3131
csfle-short = "CSFLE"
32-
csfle-long = "Client-side Field Level Encryption"
32+
csfle-long = "Client-Side Field Level Encryption"
3333
mdb-server = "MongoDB Server"

source/compatibility.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ The first column lists the driver version.
2222

2323
.. sharedinclude:: dbx/compatibility-table-legend.rst
2424

25-
.. include:: /includes/mongodb-compatibility-table-go.rst
25+
.. sharedinclude:: dbx/mongodb-compatibility-table-go.rst
2626

2727
Language Compatibility
2828
----------------------
2929

30-
.. include:: /includes/language-compatibility-table-go.rst
30+
.. sharedinclude:: dbx/language-compatibility-table-go.rst
3131

3232
For more information on how to read the compatibility tables, see our guide on
3333
:ref:`MongoDB Compatibility Tables. <about-driver-compatibility>`

source/fundamentals/indexes.txt

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,100 @@ field in the ``sample_mflix.movies`` collection:
243243

244244
Name of Index Created: cast_-1
245245

246+
.. _golang-atlas-search-indexes:
247+
248+
Atlas Search and Atlas Vector Search Indexes
249+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
250+
251+
You can programmatically manage your Atlas Search and Atlas Vector
252+
Search indexes by using the {+driver-short+}.
253+
254+
The Atlas Search feature enables you to perform full-text searches on
255+
collections hosted on MongoDB Atlas. To learn more about Atlas
256+
Search, see the :atlas:`Atlas Search
257+
</atlas-search/atlas-search-overview/>` documentation.
258+
259+
Atlas Vector Search enables you to perform semantic searches on vector
260+
embeddings stored in Atlas. To learn more about Atlas
261+
Vector Search, see the :atlas:`Atlas Vector Search
262+
</atlas-vector-search/vector-search-overview/>` documentation.
263+
264+
.. Add when Go AVS guide is ready:
265+
.. To learn more about Atlas Vector Search, see the :ref:`golang-atlas-vector-search` guide.
266+
267+
The following sections contain code examples that demonstrate how to manage Atlas
268+
Search and Atlas Vector Search indexes.
269+
270+
Create a Search Index
271+
`````````````````````
272+
273+
You can create an Atlas Search or an Atlas Vector Search index by providing
274+
an index definition to the ``SearchIndexView.CreateOne()`` method.
275+
276+
The following example creates an Atlas Search index on the ``plot`` field of the
277+
``sample_mflix.movies`` collection:
278+
279+
.. literalinclude:: /includes/fundamentals/code-snippets/indexes/atlasVectorSearch.go
280+
:language: go
281+
:start-after: start-create-atlas-search
282+
:end-before: end-create-atlas-search
283+
:dedent:
284+
285+
The following example creates an Atlas Vector Search index on the ``plot_embedding``
286+
field in the ``sample_mflix.embedded_movies`` collection:
287+
288+
.. literalinclude:: /includes/fundamentals/code-snippets/indexes/atlasVectorSearch.go
289+
:language: go
290+
:start-after: start-create-vector-search
291+
:end-before: end-create-vector-search
292+
:dedent:
293+
294+
List a Search Index
295+
```````````````````
296+
297+
You can use the ``SearchIndexView.List()`` method to list an Atlas Search or Atlas
298+
Vector Search index by specifying the name of the index.
299+
300+
The following example lists the details of the specified Atlas Search or Atlas
301+
Vector Search index:
302+
303+
.. literalinclude:: /includes/fundamentals/code-snippets/indexes/atlasVectorSearch.go
304+
:language: go
305+
:start-after: start-list-index
306+
:end-before: end-list-index
307+
:dedent:
308+
309+
Update a Search Index
310+
`````````````````````
311+
312+
You can use the ``SearchIndexView.UpdateOne()`` method to update an Atlas Search
313+
or Atlas Vector Search index by specifying the name of the index and the new
314+
index definition.
315+
316+
The following example updates an Atlas Vector Search index by providing the name
317+
of the index and a new index definition:
318+
319+
.. literalinclude:: /includes/fundamentals/code-snippets/indexes/atlasVectorSearch.go
320+
:language: go
321+
:start-after: start-update-index
322+
:end-before: end-update-index
323+
:dedent:
324+
325+
Delete a Search Index
326+
`````````````````````
327+
328+
You can use the ``SearchIndexView.DropOne()`` method to delete an Atlas Search or
329+
Atlas Vector Search index by specifying the name of the index.
330+
331+
The following example deletes an Atlas Search or Atlas Vector Search
332+
index with the specified name:
333+
334+
.. literalinclude:: /includes/fundamentals/code-snippets/indexes/atlasVectorSearch.go
335+
:language: go
336+
:start-after: start-delete-index
337+
:end-before: end-delete-index
338+
:dedent:
339+
246340
.. _golang-clustered-indexes:
247341

248342
Clustered Indexes
@@ -483,14 +577,16 @@ guides:
483577
API Documentation
484578
~~~~~~~~~~~~~~~~~
485579

486-
To learn more about any of the methods discussed in this
487-
guide, see the following API Documentation:
580+
To learn more about the methods discussed in this
581+
guide and related ones, see the following API Documentation:
488582

489583
- `IndexModel <{+api+}/mongo#IndexModel>`__
490-
- `CreateOne() <{+api+}/mongo#IndexView.CreateOne>`__
491584
- `IndexOptions <{+api+}/mongo/options#IndexOptions>`__
492585
- `SetDefaultLanguage()
493586
<{+api+}/mongo/options#IndexOptionsBuilder.SetDefaultLanguage>`__
587+
- `CreateOne() <{+api+}/mongo#IndexView.CreateOne>`__
494588
- `DropOne() <{+api+}/mongo#IndexView.DropOne>`__
495589
- `CreateCollection() <{+api+}/mongo#Database.CreateCollection>`__
496590
- `CreateCollectionOptions <{+api+}/mongo/options#CreateCollectionOptions>`__
591+
- `SearchIndexes <{+api+}/mongo#Collection.SearchIndexes>`__
592+
- `SearchIndexView <{+api+}/mongo#SearchIndexView>`__
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"go.mongodb.org/mongo-driver/v2/bson"
11+
"go.mongodb.org/mongo-driver/v2/mongo"
12+
"go.mongodb.org/mongo-driver/v2/mongo/options"
13+
)
14+
15+
func main() {
16+
ctx := context.Background()
17+
18+
// Retrieves your Atlas connection string
19+
uri := os.Getenv("MONGODB_ATLAS_URI")
20+
if uri == "" {
21+
log.Fatal("MONGODB_ATLAS_URI environment variable is not set")
22+
}
23+
24+
// Connect to your Atlas cluster
25+
clientOptions := options.Client().ApplyURI(uri)
26+
client, err := mongo.Connect(ctx, clientOptions)
27+
if err != nil {
28+
log.Fatalf("Failed to connect to the server: %v", err)
29+
}
30+
defer func() {
31+
if err := client.Disconnect(ctx); err != nil {
32+
log.Fatalf("Failed to disconnect: %v", err)
33+
}
34+
}()
35+
36+
// Set the namespace
37+
coll := client.Database("sample_mflix").Collection("embedded_movies")
38+
39+
// start-create-vector-search
40+
// Defines the structs used for the index definition
41+
type vectorDefinitionField struct {
42+
Type string `bson:"type"`
43+
Path string `bson:"path"`
44+
NumDimensions int `bson:"numDimensions"`
45+
Similarity string `bson:"similarity"`
46+
Quantization string `bson:"quantization"`
47+
}
48+
49+
type vectorDefinition struct {
50+
Fields []vectorDefinitionField `bson:"fields"`
51+
}
52+
53+
// Sets the index name and type to "vectorSearch"
54+
const indexName = "vector_search_index"
55+
opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch")
56+
57+
// Defines the index definition
58+
vectorSearchIndexModel := mongo.SearchIndexModel{
59+
Definition: vectorDefinition{
60+
Fields: []vectorDefinitionField{{
61+
Type: "vector",
62+
Path: "plot_embedding",
63+
NumDimensions: 1536,
64+
Similarity: "dotProduct",
65+
Quantization: "scalar"}},
66+
},
67+
Options: opts,
68+
}
69+
70+
// Creates the index
71+
searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, vectorSearchIndexModel)
72+
if err != nil {
73+
log.Fatalf("Failed to create the Atlas Vector Search index: %v", err)
74+
}
75+
// end-create-vector-search
76+
77+
// Creates an Atlas Search index
78+
// start-create-atlas-search
79+
// Sets the index name and type to "search"
80+
const indexName = "search_index"
81+
opts := options.SearchIndexes().SetName(indexName).SetType("search")
82+
83+
// Defines the index definition
84+
searchIndexModel := mongo.SearchIndexModel{
85+
Definition: bson.D{
86+
{Key: "mappings", Value: bson.D{
87+
{Key: "dynamic", Value: false},
88+
{Key: "fields", Value: bson.D{
89+
{Key: "plot", Value: bson.D{
90+
{Key: "type", Value: "string"},
91+
}},
92+
}},
93+
}},
94+
},
95+
Options: opts,
96+
}
97+
98+
// Creates the index
99+
searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, searchIndexModel)
100+
if err != nil {
101+
log.Fatalf("Failed to create the Atlas Search index: %v", err)
102+
}
103+
// end-create-atlas-search
104+
105+
// start-list-index
106+
// Specifies the index to retrieve
107+
const indexName = "myIndex"
108+
opts := options.SearchIndexes().SetName(indexName)
109+
110+
// Retrieves the details of the specified index
111+
cursor, err := coll.SearchIndexes().List(ctx, opts)
112+
113+
// Prints the index details to the console as JSON
114+
var results []bson.D
115+
if err := cursor.All(ctx, &results); err != nil {
116+
log.Fatalf("Failed to unmarshal results to bson: %v", err)
117+
}
118+
res, err := json.Marshal(results)
119+
if err != nil {
120+
log.Fatalf("Failed to marshal results to json: %v", err)
121+
}
122+
fmt.Println(res)
123+
// end-list-index
124+
125+
// start-update-index
126+
// Specifies the index name and the new index definition
127+
const indexName = "vector_search_index"
128+
129+
type vectorDefinitionField struct {
130+
Type string `bson:"type"`
131+
Path string `bson:"path"`
132+
NumDimensions int `bson:"numDimensions"`
133+
Similarity string `bson:"similarity"`
134+
}
135+
136+
type vectorDefinition struct {
137+
Fields []vectorDefinitionField `bson:"fields"`
138+
}
139+
140+
definition := vectorDefinition{
141+
Fields: []vectorDefinitionField{
142+
{
143+
Type: "vector",
144+
Path: "plot_embedding",
145+
NumDimensions: 1536,
146+
Similarity: "cosine",
147+
Quantization: "scalar",
148+
},
149+
},
150+
}
151+
152+
// Updates the specified index
153+
err := coll.SearchIndexes().UpdateOne(ctx, indexName, definition)
154+
if err != nil {
155+
log.Fatalf("Failed to update the index: %v", err)
156+
}
157+
// end-update-index
158+
159+
// start-delete-index
160+
// Deletes the specified index
161+
err := coll.SearchIndexes().DropOne(ctx, "myIndex")
162+
if err != nil {
163+
log.Fatalf("Failed to delete the index: %v", err)
164+
}
165+
// end-delete-index
166+
167+
}

source/includes/mongodb-compatibility-table-go.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414
- MongoDB 4.0
1515
- MongoDB 3.6
1616

17+
* - 2.1
18+
- ✓
19+
- ✓
20+
- ✓
21+
- ✓
22+
- ✓
23+
- ✓
24+
- ✓
25+
- ✓
26+
- ✓
27+
1728
* - 1.12 to 2.0
1829
- ⊛ [#8.0-support]_
1930
- ✓
@@ -36,4 +47,4 @@
3647
- ✓
3748
- ✓
3849

39-
.. [#8.0-support] {+driver-short+} v1.17 is partially compatible with {+mdb-server+} 8.0 but does not support client bulk write.
50+
.. [#8.0-support] {+driver-short+} v1.17 and v2.0 are partially compatible with {+mdb-server+} 8.0 but do not support client bulk write.

0 commit comments

Comments
 (0)