문서 메뉴
문서 홈
/ / /
Go 드라이버

빠른 참조

이 페이지에서는 여러 MongoDB 명령의 드라이버 구문과 관련 참조 및 API 문서에 대한 링크를 보여줍니다.

명령
구문
문서 찾기

API 문서
사용 예시
err = coll.FindOne(context.TODO(), bson.D{{"firstName", Mike}}).Decode(&result)
cursor, err := coll.Find(context.TODO(), bson.D{{"age", bson.D{{"$gte", 46}}}})
result, err := coll.InsertOne(
context.TODO(),
bson.D{
{"animal", "Dog"},
{"breed", "Beagle"}
}
)
여러 문서를 삽입합니다.

docs := []interface{} {
bson.D{{"firstName", "Erik"}, {"age", 27}},
bson.D{{"firstName", "Mohammad"}, {"lastName", "Ahmad"}, {"age", 10}},
bson.D{{"firstName", "Todd"}},
bson.D{{"firstName", "Juan"}, {"lastName", "Pablo"}}
}
result, err := coll.InsertMany(context.TODO(), docs)
result, err := coll.UpdateOne(
context.TODO(),
bson.D{{"firstName", "Erik"}},
bson.D{{"$set", bson.D{{"age", 28}}}}
)
fmt.Printf("The number of modified documents: %d\n", result.ModifiedCount)
여러 문서 업데이트하기

result, err := coll.UpdateMany(
context.TODO(),
bson.D{{"age", bson.D{{"$gte", 58}}}},
bson.D{{"$set", bson.D{{"description", "Senior"}}}}
)
fmt.Printf("The number of modified documents: %d\n", result.ModifiedCount)
문서 배열 업데이트

result, err := coll.UpdateMany(
context.TODO(),
bson.D{},
bson.D{{"$push", bson.D{{family, "brother"}}}}
)
result, err := coll.ReplaceOne(
context.TODO(),
bson.D{{"firstName", "Mick"}},
bson.D{{"firstName", "Mike"}, {"lastName", "Doe"}}
)
result, err := coll.DeleteOne(
context.TODO(),
bson.D{{"firstName", "Xiao"}}
)
results, err := coll.DeleteMany(
context.TODO(),
bson.D{{"age", bson.D{{"$lte", 12}}}}
)
models := []mongo.WriteModel{
mongo.NewInsertOneModel().SetDocument(bson.D{{"firstName", "John"}, {"age", 5}}),
mongo.NewUpdateOneModel().SetFilter(bson.D{{"firstName", "Juan"}}).
SetUpdate(bson.D{{"$set", bson.D{{"age", 12}}}}),
}
opts := options.BulkWrite().SetOrdered(true)
results, err := coll.BulkWrite(context.TODO(), models, opts)
데이터 변경 사항 모니터링

pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{{"operationType", "insert"}}}}}
cs, err := coll.Watch(context.TODO(), pipeline)
커서에서 반복적으로 데이터에 액세스

cursor, err := coll.Find(context.TODO(), bson.D{})
for cursor.Next(context.TODO()) {
var result bson.D
if err := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
커서에서 배열로 데이터에 액세스

cursor, err := coll.Find(context.TODO(), bson.D{})
var results []bson.D
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
count, err := coll.CountDocuments(context.TODO(), bson.D{})
고유 문서 또는 필드 값 나열
results, err := coll.Distinct(context.TODO(), "firstName", bson.D{})
조회되는 문서 수 제한

cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetLimit(2))
조회된 문서 건너뛰기

// the collection has 6 documents
cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSkip(4))
조회 시 문서 정렬

cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSort(bson.D{{"age", 1}}))
조회 시 문서 필드 프로젝트

cursor, err := coll.Find(
context.TODO(),
bson.D{},
options.Find().SetProjection(
bson.D{{"age", 0}, {"_id",0}}
)
)
인덱스 만들기

model := mongo.IndexModel{Keys: bson.D{{"firstName", 1}, {"lastName", -1}}}
name, err := coll.Indexes().CreateOne(context.TODO(), model)
텍스트 검색

// only searches fields with text indexes
cursor, err := coll.Find(context.TODO(), bson.D{{"$text", bson.D{{"$search", "beagle"}}}})
← 빠른 시작