You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

doc.go 3.2 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // NOTE: This documentation should be kept in line with the Example* test functions.
  7. // Package mongo provides a MongoDB Driver API for Go.
  8. //
  9. // Basic usage of the driver starts with creating a Client from a connection
  10. // string. To do so, call the NewClient and Connect functions:
  11. //
  12. // client, err := NewClient(options.Client().ApplyURI("mongodb://foo:bar@localhost:27017"))
  13. // if err != nil { return err }
  14. // ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  15. // defer cancel()
  16. // err = client.Connect(ctx)
  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. // Methods that only return a single document will return a *SingleResult, which works
  54. // like a *sql.Row:
  55. //
  56. // result := struct{
  57. // Foo string
  58. // Bar int32
  59. // }{}
  60. // filter := bson.D{{"hello", "world"}}
  61. // err := collection.FindOne(context.Background(), filter).Decode(&result)
  62. // if err != nil { return err }
  63. // // do something with result...
  64. //
  65. // All Client, Collection, and Database methods that take parameters of type interface{}
  66. // will return ErrNilDocument if nil is passed in for an interface{}.
  67. //
  68. // Additional examples can be found under the examples directory in the driver's repository and
  69. // on the MongoDB website.
  70. //
  71. // Potential DNS Issues
  72. //
  73. // Building with Go 1.11+ and using connection strings with the "mongodb+srv"[1] scheme is
  74. // incompatible with some DNS servers in the wild due to the change introduced in
  75. // https://github.com/golang/go/issues/10622. If you receive an error with the message "cannot
  76. // unmarshal DNS message" while running an operation, we suggest you use a different DNS server.
  77. //
  78. // [1] See https://docs.mongodb.com/manual/reference/connection-string/#dns-seedlist-connection-format
  79. package mongo