AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

クイック リファレンス

このページでは、いくつかの MongoDB コマンドのドライバー構文を示し、それらに関連するリファレンスと API ドキュメントにリンクします。

コマンド
構文
err = coll.FindOne(context.TODO(), bson.D{{"firstName", Mike}}).Decode(&result)
[{firstName Mike}, {lastName Smith} ...]

複数ドキュメントの検索

API ドキュメント
使用例
Fundamentals

cursor, err := coll.Find(context.TODO(), bson.D{{"age", bson.D{{"$gte", 46}}}})
[{firstName Kyle}, {age 51}, ... ]
[{firstName Omar}, {age 47}, ... ]
result, err := coll.InsertOne(
context.TODO(),
bson.D{
{"animal", "Dog"},
{"breed", "Beagle"}
}
)

複数のドキュメントの挿入

API ドキュメント
使用例
Fundamentals

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)
The number of modified documents: 1

複数のドキュメントの更新

API ドキュメント
使用例
Fundamentals

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)
The number of modified documents: 4

ドキュメント内の配列を更新

API ドキュメント
Fundamentals

result, err := coll.UpdateMany(
context.TODO(),
bson.D{},
bson.D{{"$push", bson.D{{family, "brother"}}}}
)
[{firstName Xiao}, {family ["brother"]}, ... ]
[{firstName Omar}, {family ["brother", "mother"]}, ... ]
...

ドキュメントの置き換え

API ドキュメント
使用例
Fundamentals

result, err := coll.ReplaceOne(
context.TODO(),
bson.D{{"firstName", "Mick"}},
bson.D{{"firstName", "Mike"}, {"lastName", "Doe"}}
)
[{{firstName Mike}, {lastName Doe} }]
result, err := coll.DeleteOne(
context.TODO(),
bson.D{{"firstName", "Xiao"}}
)

複数のドキュメントの削除

API ドキュメント
使用例
Fundamentals

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)
[{firstName John}, {age 5} ... ]
[{firstName Juan}, {age 12} ... ]

データの変更を監視

API ドキュメント
使用例

pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{{"operationType", "insert"}}}}}
cs, err := coll.Watch(context.TODO(), pipeline)

カーソルから反復的にデータにアクセスする

API ドキュメント
Fundamentals

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)
}
[{firstName Doug} ... ]
[{firstName Erik} ...]
[{lastName Chang} ...]
...

カーソルから配列としてカーソルにアクセス

API ドキュメント
Fundamentals

cursor, err := coll.Find(context.TODO(), bson.D{})
var results []bson.D
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
[{name Mike} ... ]
[{name Edgar} ...]
[{name Freddie} ...]
...

ドキュメントをカウント

API ドキュメント
使用例
Fundamentals

count, err := coll.CountDocuments(context.TODO(), bson.D{})
6

個別のドキュメントまたはフィールド値を一覧表示する
API ドキュメント
使用例
Fundamentals

results, err := coll.Distinct(context.TODO(), "firstName", bson.D{})
Mike
Xiao
...

取得されるドキュメント数の制限

API ドキュメント
Fundamentals

cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetLimit(2))
[{breed Beagle} ... ]
[{breed German Shepard} ...]

検索されたドキュメントをスキップする

API ドキュメント
Fundamentals

// the collection has 6 documents
cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSkip(4))
[{item Pen} ... ]
[{item Chair} ...]

ドキュメントを取得するときにソートする

API ドキュメント
Fundamentals

cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSort(bson.D{{"age", 1}}))
[{firstName Dev} {age 5} ... ]
[{firstName Jose} {age 7} ... ]
[{firstName Om} {age 8} ... ]

プロジェクト ドキュメント フィールドを取得する際

API ドキュメント
Fundamentals

cursor, err := coll.Find(
context.TODO(),
bson.D{},
options.Find().SetProjection(
bson.D{{"age", 0}, {"_id",0}}
)
)
[{firstName Lester}]
[{firstName Wendall} {lastName Griffin}]
...

インデックスの作成

API ドキュメント
Fundamentals

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"}}}})
[{"firstName": "Emily" , "Description": "I love to play sports and walk my beagle."} ... ]