<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] > [github.com](https://github.com/mongodb/mongo-go-driver) ## 連接 ``` xxx.db('mongodb://[yourname: your_pwd@]ip:27017[/Article]'); ``` ## 結構類型 ## 連接 ``` client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://root:123456@localhost:27017")) if err != nil { panic(err ) } ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) err = client.Connect(ctx) if err != nil { panic(err) } err = client.Ping(ctx, readpref.Primary()) if err != nil { panic(err) } //連接庫名,連接集合名 collection := client.Database("demo").Collection("numbers") ``` ## 創建索引 ### 創建單個索引 ``` coll := mon.Database(databases).Collection(collection) index := mongo.IndexModel{ Keys: bson.M{ "msg_id": "", // "recv_id": "",/創建單索引多字段 }, Options: options.Index().SetSparse(true),//需要在集合中某字段創建索引,但集合中大量的文檔不包含此鍵值時,建議創建稀疏索引。 } many, err := coll.Indexes().CreateOne(context.Background(), index) if err != nil { log.Errorf("many []string:%v err:%v\n",many,err) } ``` ### 創建多個索引 ``` coll := db.Collection("test") index := []mongo.IndexModel{ { Keys: bsonx.Doc{{Key: "name", Value: bsonx.String("text")}}, }, { Keys: bsonx.Doc{{Key: "createdAt", Value: bsonx.Int32(-1)}}, }, } opts := options.CreateIndexes().SetMaxTime(10 * time.Second) _, errIndex = coll.Indexes().CreateMany(context, index, opts) if err != nil { panic(errIndex) } ``` ## 插入 ### 插入單挑條 ``` //連接庫名,連接集合名 collection := client.Database("demo").Collection("numbers") //mongodb能識別出類型 result, err := collection.InsertOne(ctx, bson.M{"name": "cc", "age": 123}) if err != nil { log.Printf("%v",err) } fmt.Printf("%+v\n", result.InsertedID) // ObjectID("5e0d65d4410d8db85702208c") ``` ### 插入多條 ``` var docs []interface{} docs = append(docs, bson.M{"name":"cpj","age":1}) docs = append(docs, bson.M{"name":"cpj2","age":2}) //mongodb能識別出類型 result, err := collection.InsertMany(ctx, docs) if err != nil { log.Printf("%v", err) } fmt.Printf("%+v\n", result.InsertedIDs) // [ObjectID("5e0d6784c70cf1dc87aaa8dc") ObjectID("5e0d6784c70cf1dc87aaa8dd")] ``` ## 查詢 ## 單結果查詢 ``` filter := bson.M{"name": "cc"} var result bson.M err = collection.FindOne(ctx, filter).Decode(&result) if err != nil { log.Printf("%v",err) } fmt.Printf("%+v\n", result) //map[_id:ObjectID("5e0d64422f5816e60c2df86a") age:1 name:cc] ``` ### 多結果條件查詢 ``` filter := bson.M{"name": "cc"} sort := bson.D{{"age", 1}} opts := options.Find(). SetLimit(2). SetSort(sort) cur, err := collection.Find(ctx, filter,opts) if err != nil { log.Fatal(err) } defer cur.Close(ctx) var results []bson.M err = cur.All(ctx, &results) if err != nil { log.Printf("%v",err) } fmt.Printf("%+v\n", results) //[map[_id:ObjectID("5e0d64422f5816e60c2df86a") age:1 name:cc] map[_id:ObjectID("5e0d65d4410d8db85702208b") age:3 name:cc]] ``` ## 文件上傳下載 ``` //指定數據庫 databases := client.Database("gridfs") bucket, err := gridfs.NewBucket(databases) filename := "go.mod1" if err != nil { log.Printf("%v", err) } //文件上傳 fileData, _ := ioutil.ReadFile(filename) ids, err := bucket.UploadFromStream(filename, bytes.NewReader(fileData)) //此處 filename 是 數據庫顯示的文件名 if err != nil { log.Printf("%v", err) } fmt.Printf("%+v\n", ids) //文件下載 var byt bytes.Buffer n, err := bucket.DownloadToStreamByName(filename, &byt) if err != nil { log.Printf("%v",err) } fmt.Printf("File size to download: %v 字節\n", n) err = ioutil.WriteFile("test1", byt.Bytes(), 0600) if err != nil { log.Printf("%v",err) } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看