Bu bölüm Golang MongoDB belge sorgularının kullanımını tanıtır.
Önkoşul Öğreticiler
İpucu: MongoDB sorgu sözdizimine aşina değilseniz önce MongoDB öğreticisini okuyun. Golang'da MongoDB işlemleri için kullanılan ifade sözdizimi aynıdır.
Test Verilerini Hazırlama
coll
koleksiyonuna bir dizi belge verisi ekleyin.
docs := []interface{}{
bson.D{
{"item", "journal"},
{"qty", 25},
{"size", bson.D{
{"h", 14},
{"w", 21},
{"uom", "cm"},
}},
{"status", "A"},
},
// ... (diğer belge verileri)
}
sonuç, hata := coll.InsertMany(context.Background(), docs)
Tüm Belgeleri Sorgulama
cursor, hata := coll.Find(
context.Background(),
bson.D{}, // Boş sorgu koşulu ayarla
)
Eşitlik Sorgu Koşulları
SQL'nin eşitlik eşleşmesine benzer.
cursor, hata := coll.Find(
context.Background(),
bson.D{{"status", "D"}}, // Eşdeğer koşul: status = D
)
in
Sorgu Operatörü
SQL'nin in
sorgusuna benzer.
cursor, hata := coll.Find(
context.Background(),
bson.D{{"status", bson.D{{"$in", bson.A{"A", "D"}}}}}) // Eşdeğer koşul: status in ('A', 'D')
and
Sorgu Koşulları
SQL'nin and
mantıksal ifadesine benzer.
cursor, hata := coll.Find(
context.Background(),
bson.D{ // Eşdeğer koşul: status='A' ve qty < 30
{"status", "A"},
{"qty", bson.D{{"$lt", 30}}},
})
or
Sorgu Koşulları
SQL'nin or
mantıksal ifadesine benzer.
cursor, hata := coll.Find(
context.Background(),
bson.D{ // Eşdeğer koşul: status = "A" YA DA qty < 30
{"$or", // $or operatörü kullanımı
bson.A{ // bson.A dizisi tipi tanımı kullanımı
bson.D{{"status", "A"}},
bson.D{{"qty", bson.D{{"$lt", 30}}}},
}},
})
Birleşik Sorgu Örneği
cursor, hata := coll.Find(
context.Background(),
bson.D{ // Eşdeğer koşul: status = "A" VE ( qty < 30 VEYA item "p%" İLE BAŞLAR)
{"status", "A"},
{"$or", bson.A{
bson.D{{"qty", bson.D{{"$lt", 30}}}},
bson.D{{"item", primitive.Regex{Pattern: "^p", Options: ""}}},
}},
})
Sorgu Sonuçlarını Gezinme
Örnek 1
// Adı 'Bob' olan belgeleri sorgula
cursor, hata := coll.Find(context.TODO(), bson.D{{"name", "Bob"}}, opts)
if hata != nil {
log.Fatal(hata)
}
// bson.M türünde belge verilerini içeren bir dizi tanımla; bson.M, bir harita gibi anahtar-değer veri yapısına benzer
var sonuçlar []bson.M
// Tüm sorgu sonuçlarını almak için All fonksiyonunu kullanarak sonuçları sonuçlar değişkenine kaydet
if hata = cursor.All(context.TODO(), &sonuçlar); hata != nil {
log.Fatal(hata)
}
// Sonuçlar dizisini dolaş
for _, sonuç := range sonuçlar {
fmt.Println(sonuç)
}
Açıklama: Örnek, belge verilerini saklamak için bson.M türünü kullanır. Alanların belgeyle eşleştiği sürece bir struct türünü özelleştirebilirsiniz.
Örnek 2
// Sorgu sonucunu saklamak için bir struct türünde değişken tanımla
var sonuç struct {
Value float64
}
// 'pi' ismine eşit olan belge sorgu koşulunu tanımla
filtre := bson.D{{"name", "pi"}}
// Bir bağlam nesnesi oluştur ve sorgu zaman aşımını 5 saniyeye ayarla
ctx, iptal = context.WithTimeout(context.Background(), 5*time.Second)
defer iptal()
// Koşula dayalı tek veri sorgula ve sonucu sonuç değişkenine kaydetmek için Decode fonksiyonunu kullan
hata = koleksiyon.FindOne(ctx, filtre).Decode(&sonuç)
// Sorgu hatalarını kontrol et
if hata == mongo.ErrNoDocuments {
// Belge bulunamadı
fmt.Println("Belge mevcut değil")
} else if hata != nil {
log.Fatal(hata)
}
// Diğer işlemler
İpucu: Daha sofistike MongoDB sorgu ifade sözdizimi için lütfen MongoDB öğreticisine başvurun.