github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/doc.go (about) 1 // Copyright (C) MongoDB, Inc. 2017-present. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 7 // NOTE: This documentation should be kept in line with the Example* test functions. 8 9 // Package mongo provides a MongoDB Driver API for Go. 10 // 11 // Basic usage of the driver starts with creating a Client from a connection 12 // string. To do so, call Connect: 13 // 14 // ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) 15 // defer cancel() 16 // client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://foo:bar@localhost:27017")) 17 // if err != nil { return err } 18 // 19 // This will create a new client and start monitoring the MongoDB server on localhost. 20 // The Database and Collection types can be used to access the database: 21 // 22 // collection := client.Database("baz").Collection("qux") 23 // 24 // A Collection can be used to query the database or insert documents: 25 // 26 // res, err := collection.InsertOne(context.Background(), bson.M{"hello": "world"}) 27 // if err != nil { return err } 28 // id := res.InsertedID 29 // 30 // Several methods return a cursor, which can be used like this: 31 // 32 // cur, err := collection.Find(context.Background(), bson.D{}) 33 // if err != nil { log.Fatal(err) } 34 // defer cur.Close(context.Background()) 35 // for cur.Next(context.Background()) { 36 // // To decode into a struct, use cursor.Decode() 37 // result := struct{ 38 // Foo string 39 // Bar int32 40 // }{} 41 // err := cur.Decode(&result) 42 // if err != nil { log.Fatal(err) } 43 // // do something with result... 44 // 45 // // To get the raw bson bytes use cursor.Current 46 // raw := cur.Current 47 // // do something with raw... 48 // } 49 // if err := cur.Err(); err != nil { 50 // return err 51 // } 52 // 53 // Cursor.All will decode all of the returned elements at once: 54 // 55 // var results []struct{ 56 // Foo string 57 // Bar int32 58 // } 59 // if err = cur.All(context.Background(), &results); err != nil { 60 // log.Fatal(err) 61 // } 62 // // do something with results... 63 // 64 // Methods that only return a single document will return a *SingleResult, which works 65 // like a *sql.Row: 66 // 67 // result := struct{ 68 // Foo string 69 // Bar int32 70 // }{} 71 // filter := bson.D{{"hello", "world"}} 72 // err := collection.FindOne(context.Background(), filter).Decode(&result) 73 // if err != nil { return err } 74 // // do something with result... 75 // 76 // All Client, Collection, and Database methods that take parameters of type interface{} 77 // will return ErrNilDocument if nil is passed in for an interface{}. 78 // 79 // Additional examples can be found under the examples directory in the driver's repository and 80 // on the MongoDB website. 81 // 82 // # Error Handling 83 // 84 // Errors from the MongoDB server will implement the ServerError interface, which has functions to check for specific 85 // error codes, labels, and message substrings. These can be used to check for and handle specific errors. Some methods, 86 // like InsertMany and BulkWrite, can return an error representing multiple errors, and in those cases the ServerError 87 // functions will return true if any of the contained errors satisfy the check. 88 // 89 // There are also helper functions to check for certain specific types of errors: 90 // 91 // IsDuplicateKeyError(error) 92 // IsNetworkError(error) 93 // IsTimeout(error) 94 // 95 // # Potential DNS Issues 96 // 97 // Building with Go 1.11+ and using connection strings with the "mongodb+srv"[1] scheme is unfortunately 98 // incompatible with some DNS servers in the wild due to the change introduced in 99 // https://github.com/golang/go/issues/10622. You may receive an error with the message "cannot unmarshal DNS message" 100 // while running an operation when using DNS servers that non-compliantly compress SRV records. Old versions of kube-dns 101 // and the native DNS resolver (systemd-resolver) on Ubuntu 18.04 are known to be non-compliant in this manner. We suggest 102 // using a different DNS server (8.8.8.8 is the common default), and, if that's not possible, avoiding the "mongodb+srv" 103 // scheme. 104 // 105 // # Client Side Encryption 106 // 107 // Client-side encryption is a new feature in MongoDB 4.2 that allows specific data fields to be encrypted. Using this 108 // feature requires specifying the "cse" build tag during compilation: 109 // 110 // go build -tags cse 111 // 112 // Note: Auto encryption is an enterprise- and Atlas-only feature. 113 // 114 // The libmongocrypt C library is required when using client-side encryption. Specific versions of libmongocrypt 115 // are required for different versions of the Go Driver: 116 // 117 // - Go Driver v1.2.0 requires libmongocrypt v1.0.0 or higher 118 // 119 // - Go Driver v1.5.0 requires libmongocrypt v1.1.0 or higher 120 // 121 // - Go Driver v1.8.0 requires libmongocrypt v1.3.0 or higher 122 // 123 // - Go Driver v1.10.0 requires libmongocrypt v1.5.0 or higher. 124 // There is a severe bug when calling RewrapManyDataKey with libmongocrypt versions less than 1.5.2. 125 // This bug may result in data corruption. 126 // Please use libmongocrypt 1.5.2 or higher when calling RewrapManyDataKey. 127 // 128 // - Go Driver v1.12.0 requires libmongocrypt v1.8.0 or higher. 129 // 130 // To install libmongocrypt, follow the instructions for your 131 // operating system: 132 // 133 // 1. Linux: follow the instructions listed at 134 // https://github.com/mongodb/libmongocrypt#installing-libmongocrypt-from-distribution-packages to install the correct 135 // deb/rpm package. 136 // 137 // 2. Mac: Follow the instructions listed at https://github.com/mongodb/libmongocrypt#installing-libmongocrypt-on-macos 138 // to install packages via brew and compile the libmongocrypt source code. 139 // 140 // 3. Windows: 141 // 142 // mkdir -p c:/libmongocrypt/bin 143 // mkdir -p c:/libmongocrypt/include 144 // 145 // // Run the curl command in an empty directory as it will create new directories when unpacked. 146 // curl https://s3.amazonaws.com/mciuploads/libmongocrypt/windows/latest_release/libmongocrypt.tar.gz --output libmongocrypt.tar.gz 147 // tar -xvzf libmongocrypt.tar.gz 148 // 149 // cp ./bin/mongocrypt.dll c:/libmongocrypt/bin 150 // cp ./include/mongocrypt/*.h c:/libmongocrypt/include 151 // export PATH=$PATH:/cygdrive/c/libmongocrypt/bin 152 // 153 // libmongocrypt communicates with the mongocryptd process or mongo_crypt shared library for automatic encryption. 154 // See AutoEncryptionOpts.SetExtraOptions for options to configure use of mongocryptd or mongo_crypt. 155 // 156 // [1] See https://www.mongodb.com/docs/manual/reference/connection-string/#dns-seedlist-connection-format 157 package mongo