返すフィールドを指定する
Overview
In this guide, you can learn how to specify which fields to return in a document from read operations.
サンプル データ
このガイドの例を実行するには、次のスニペットを使用してこれらのドキュメントをtea.ratings
コレクションにロードします。
coll := client.Database("tea").Collection("ratings") docs := []interface{}{ bson.D{{"type", "Masala"}, {"rating", 10}}, bson.D{{"type", "Assam"}, {"rating", 5}}, bson.D{{"type", "Oolong"}, {"rating", 7}}, bson.D{{"type", "Earl Grey"}, {"rating", 8}}, bson.D{{"type", "English Breakfast"}, {"rating", 5}}, } result, err := coll.InsertMany(context.TODO(), docs) if err != nil { panic(err) } fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
Tip
存在しないデータベースとコレクション
書き込み操作を実行するときに必要なデータベースとコレクションが存在しない場合は、サーバーが暗黙的にそれらを作成します。
各ドキュメントには、 フィールドとtype
rating
フィールドに対応するお茶の種類の評価が含まれています。
注意
各例ではObjectID
値が切り捨てられます。この値はドライバーが個別に生成するためです。
プロジェクション
A projection specifies which fields to return in matched documents. It
contains field names followed by a 1
(to include) or 0
(to
exclude). Projections can only include or exclude fields.
You can specify a projection by passing one to the SetProjection()
method in the options of the following read operation methods:
Find()
FindOne()
FindOneAndDelete()
FindOneAndReplace()
FindOneAndUpdate()
Tip
If you don't specify a projection, the read operation returns all the fields in matched documents.
フィールドを除外する
To exclude a field, pass the field you want to exclude and a 0
to
the SetProjection()
method. For all fields you don't explicitly
list in the projection, the driver includes them.
例
The following example excludes the rating
from the matched documents
from the Find()
method:
opts := options.Find().SetProjection(bson.D{{"rating", 0}}) cursor, err := coll.Find(context.TODO(), bson.D{}, opts) if err != nil { panic(err) } var results []bson.D if err = cursor.All(context.TODO(), &results); err != nil { panic(err) } for _, result := range results { fmt.Println(result) }
フィールドを含める
To include a field, pass the field you want to include and a 1
to
the SetProjection()
method. For all fields you don't explicitly
list in the projection, the driver excludes them.
重要
You can exclude the _id
field even if you specified to include
certain fields. By default, the driver includes the _id
field.
You must explicitly exclude the _id
field if you do not want it
returned.
例
The following example performs the following projection on the matched
documents from the Find()
method:
Include the
type
andrating
fieldExclude the
_id
field
opts := options.Find().SetProjection(bson.D{{"type", 1}, {"rating", 1}, {"_id", 0}}) cursor, err := coll.Find(context.TODO(), bson.D{}, opts) if err != nil { panic(err) } var results []bson.D if err = cursor.All(context.TODO(), &results); err != nil { panic(err) } for _, result := range results { fmt.Println(result) }
集計
You can also include the $project stage to specify a projection in an aggregation pipeline.
例
The following example performs the following projection on the matched
documents from the Aggregate()
method:
Include the
type
andrating
fieldExclude the
_id
field
projectStage := bson.D{{"$project", bson.D{{"type", 1}, {"rating", 1}, {"_id", 0}}}} cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{projectStage}) if err != nil { panic(err) } var results []bson.D if err = cursor.All(context.TODO(), &results); err != nil { panic(err) } for _, result := range results { fmt.Println(result) }
詳細情報
上記で説明されている操作の詳細については、次のガイドを参照してください。
テキスト検索からテキスト スコアを予測する方法については、「テキストを検索する
API ドキュメント
このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。