<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國際加速解決方案。 廣告
                > # MongoDB - 概念 - 數據庫 - 集合 - 文檔 ~~~ package main import ( "context" "fmt" "log" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { // 設置 MongoDB 連接選項 clientOptions := options.Client().ApplyURI("mongodb://localhost:27017") client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { log.Fatal(err) } // 檢查連接是否正常 err = client.Ping(context.TODO(), nil) if err != nil { log.Fatal(err) } fmt.Println("Connected to MongoDB!") // 選擇要操作的數據庫和集合 collection := client.Database("testdb").Collection("testcollection") /* // 插入文檔(單個) insertDoc := bson.D{ {Key: "name", Value: "John"}, {Key: "age", Value: 30}, {Key: "city", Value: "New York"}, } */ /* //插入文檔(多個) insertDoc := []interface{}{ bson.D{ {Key: "name", Value: "Test1"}, {Key: "age", Value: 30}, }, bson.D{ {Key: "name", Value: "Test2"}, {Key: "age", Value: 25}, }, } _, err = collection.InsertMany(context.TODO(), insertDoc) if err != nil { log.Fatal(err) } */ /* // 查詢文檔(單個) var result bson.M //設置查詢過濾條件 filter := bson.D{{"name", "Test1"}} err = collection.FindOne(context.TODO(), filter).Decode(&result) if err != nil { log.Fatal(err) } fmt.Println("Found document:", result) */ /* // 查詢文檔(多個) type Person struct { Name string Age int } // 構建正則表達式來進行模糊搜索 regexPattern := "1.*" // 這里使用 "John" 開頭的字符串作為模糊搜索的示例 // 創建正則表達式 regex := primitive.Regex{Pattern: regexPattern, Options: "i"} // "i" 表示不區分大小寫 // 設置查詢過濾條件(查詢年齡大于等于 >=25 && <= 100 歲 的文檔) filter := bson.D{ {"name", bson.M{"$regex": regex}}, // 設置查詢條件,使用正則表達式匹配名字字段 {"age", bson.D{{"$gte", 25}, {"$lte", 100}}}, } // 設置排序條件,按年齡升序排列 options := options.Find() options.SetSort(bson.D{{"age", 1}}) //1 表示升序,-1 表示降序 // 設置結果數量限制 options.SetLimit(10) // 這里限制結果數量為 10 // 執行查詢 cur, err := collection.Find(context.TODO(), filter, options) if err != nil { log.Fatal(err) } defer cur.Close(context.TODO()) // 使用切片的指針來存儲查詢結果 var results []Person // 遍歷查詢結果并解碼 for cur.Next(context.TODO()) { var result Person err := cur.Decode(&result) if err != nil { log.Fatal(err) } results = append(results, result) } if err := cur.Err(); err != nil { log.Fatal(err) } */ /* // 更新文檔 filter := bson.D{{"name", "Test1"}} update := bson.D{ {"$set", bson.D{{"age", 55}}}, } updateResult, err := collection.UpdateMany(context.TODO(), filter, update) if err != nil { log.Fatal(err) } fmt.Printf("Matched %v documents and modified %v documents\n", updateResult.MatchedCount, updateResult.ModifiedCount) */ // 刪除文檔 filter := bson.D{{"name", "Test1"}} deleteResult, err := collection.DeleteMany(context.TODO(), filter) if err != nil { log.Fatal(err) } fmt.Printf("Deleted %v documents in the collection\n", deleteResult.DeletedCount) // 斷開連接 err = client.Disconnect(context.TODO()) if err != nil { log.Fatal(err) } fmt.Println("Connection to MongoDB closed.") // 等待一段時間以確保連接被關閉(可選) time.Sleep(2 * time.Second) } ~~~ MongoDB支持多種索引類型,以滿足不同的查詢和性能需求。以下是一些常見的MongoDB索引類型: 1. **單字段索引(Single Field Index):** 最簡單的索引類型,對單個字段進行索引,可以是升序或降序。 2. **復合索引(Compound Index):** 在多個字段上創建組合索引,可以更好地支持復雜的查詢。復合索引可以包含多個字段,并按照指定的順序創建。 3. **文本索引(Text Index):** 用于支持文本搜索,可以在文本字段上創建,允許全文本搜索和文本分析。 4. **地理空間索引(Geospatial Index):** 用于地理空間數據(如地理坐標),可以用于地理位置查詢和地理位置距離計算。 5. **哈希索引(Hash Index):** 用于散列字段,對字段進行哈希索引,可用于提高散列字段的查詢性能。 6. **唯一索引(Unique Index):** 用于確保集合中的字段具有唯一值,重復值將被拒絕插入。 7. **部分索引(Partial Index):** 僅索引滿足特定條件的文檔,可用于過濾數據。 8. **TTL(Time-To-Live)索引:** 用于自動刪除具有指定過期時間的文檔,可用于實現數據自動清理。 9. **復合文本索引(Compound Text Index):** 用于在多個文本字段上創建組合文本索引,以支持復雜的全文本搜索。 10. **復合地理空間索引(Compound Geospatial Index):** 在多個地理空間字段上創建組合地理空間索引,用于更復雜的地理查詢。 11. **全文本索引(Wildcard Text Index):** 用于全文搜索,通常與文本搜索操作符一起使用。 這些索引類型可以根據您的數據和查詢需求進行組合使用,以提高查詢性能和效率。不同的索引類型適用于不同的用例,因此在設計數據庫模式時需要仔細考慮索引的選擇和使用。 ``` import ( "context" "fmt" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { // 設置MongoDB連接選項 clientOptions := options.Client().ApplyURI("mongodb://localhost:27017") // 連接到MongoDB client, err := mongo.Connect(context.Background(), clientOptions) if err != nil { fmt.Println("連接到MongoDB失敗:", err) return } // 選擇要創建索引的集合 collection := client.Database("your_database").Collection("your_collection") // 定義要在哪些字段上創建索引,示例為單個字段的升序索引 indexModel := mongo.IndexModel{ Keys: bson.M{ "your_field": 1, // 1表示升序,-1表示降序 }, } // 創建索引 _, err = collection.Indexes().CreateOne(context.Background(), indexModel) if err != nil { fmt.Println("創建索引失敗:", err) return } fmt.Println("索引創建成功") } ```
                  <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>

                              哎呀哎呀视频在线观看