| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package mongodocstore_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"log" |
| 20 |
|
| 21 |
"gocloud.dev/docstore" |
| 22 |
"gocloud.dev/docstore/mongodocstore" |
| 23 |
) |
| 24 |
|
| 25 |
func ExampleOpenCollection() { |
| 26 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 27 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 28 |
ctx := context.Background() |
| 29 |
|
| 30 |
client, err := mongodocstore.Dial(ctx, "mongodb://my-host") |
| 31 |
if err != nil { |
| 32 |
log.Fatal(err) |
| 33 |
} |
| 34 |
mcoll := client.Database("my-db").Collection("my-coll") |
| 35 |
coll, err := mongodocstore.OpenCollection(mcoll, "userID", nil) |
| 36 |
if err != nil { |
| 37 |
log.Fatal(err) |
| 38 |
} |
| 39 |
defer coll.Close() |
| 40 |
} |
| 41 |
|
| 42 |
func ExampleOpenCollectionWithIDFunc() { |
| 43 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 44 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 45 |
ctx := context.Background() |
| 46 |
type HighScore struct { |
| 47 |
Game string |
| 48 |
Player string |
| 49 |
} |
| 50 |
|
| 51 |
client, err := mongodocstore.Dial(ctx, "mongodb://my-host") |
| 52 |
if err != nil { |
| 53 |
log.Fatal(err) |
| 54 |
} |
| 55 |
mcoll := client.Database("my-db").Collection("my-coll") |
| 56 |
|
| 57 |
// The name of a document is constructed from the Game and Player fields. |
| 58 |
nameFromDocument := func(doc docstore.Document) interface{} { |
| 59 |
hs := doc.(*HighScore) |
| 60 |
return hs.Game + "|" + hs.Player |
| 61 |
} |
| 62 |
|
| 63 |
coll, err := mongodocstore.OpenCollectionWithIDFunc(mcoll, nameFromDocument, nil) |
| 64 |
if err != nil { |
| 65 |
log.Fatal(err) |
| 66 |
} |
| 67 |
defer coll.Close() |
| 68 |
} |
| 69 |
|
| 70 |
func Example_openCollectionFromURL() { |
| 71 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 72 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/docstore/mongodocstore" |
| 73 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 74 |
ctx := context.Background() |
| 75 |
|
| 76 |
// docstore.OpenCollection creates a *docstore.Collection from a URL. |
| 77 |
coll, err := docstore.OpenCollection(ctx, "mongo://my-db/my-collection?id_field=userID") |
| 78 |
if err != nil { |
| 79 |
log.Fatal(err) |
| 80 |
} |
| 81 |
defer coll.Close() |
| 82 |
} |