6

質問への2つの部分。

1はmongodbクエリそのもの、次はmgoでのやり方です。

タイプ カテゴリの 1 つのドキュメントを照会するにはどうすればよいですか (結果はタイプ カテゴリである必要があります) slug: "general"

このレイアウトを選んだ理由は、mongodb の利点は組み込みの「構造体」を使用した場合のパフォーマンスであると読んだためですが、「カテゴリ」と「フォーラム」を独自のコレクションにし、多くのコードを書き直す必要があるのではないかと心配しています。避けたいと思います。とにかく、クライアント側のすべてのビューがこれらのモデルにアクセスする必要があり、新しいページの読み込みごとに (カテゴリとフォーラムの) 1 ~ 2 個のクエリが追加され、mongodb を使用する利点が失われるためです。

フォローアップの質問は、特定の埋め込みドキュメントを更新または削除するにはどうすればよいですか?

ドキュメントを分離したり、 Go で find 、 update 、 delete 関数を書いたりする必要なく、mongodb からカテゴリドキュメントを直接取得する方法はありますか?

この構造:

{
    "_id" : ObjectId("5303d1a2d6194c0f27000001"),
    "name" : "darko",
    "description" : "darko",
    "subdomain" : "darko",
    "domain" : "mango.dev",
    "created" : ISODate("2014-02-18T21:33:22.115Z"),
    "category" : "Brains",
    "owner" : "52b1d74dd6194c0646000002",
    "members" : [ 
        "52b1d74dd6194c0646000002"
    ],
    "categories" : [ 
        {
            "_id" : ObjectId("5303d1a2d6194c0f27000003"),
            "name" : "Admin and Moderator Area",
            "slug" : "admin-and-moderator-area",
            "adminonly" : true,
            "membersonly" : false,
            "forums" : [ 
                {
                    "_id" : ObjectId("5303d1a2d6194c0f27000005"),
                    "name" : "Admin Discussion",
                    "slug" : "admin-discussion",
                    "text" : "This is the main forum for administrative topics."
                }
            ]
        }, 
        {
            "_id" : ObjectId("5303d1a2d6194c0f27000002"),
            "name" : "General",
            "slug" : "general",
            "adminonly" : false,
            "membersonly" : false,
            "forums" : [ 
                {
                    "_id" : ObjectId("5303d1a2d6194c0f27000004"),
                    "name" : "General Discussion",
                    "slug" : "general-discussion",
                    "text" : "Talk about everything and anything here in this general discussion forum"
                }
            ]
        }
    ]
}

または行く:

Community struct {
    Id          bson.ObjectId `bson:"_id,omitempty" json:"id"`
    Name        string        `json:"name"`
    Description string        `bson:",omitempty" json:"description"`
    Subdomain   string        `bson:",omitempty" json:"subdomain"`
    Domain      string        `json:"domain"`
    Created     time.Time     `json:"created"`
    Category    string        `json:"category"`
    Owner       interface{}   `json:"owner"`                         //userid
    Members     []interface{} `json:"members"`                       //userid
    Moderators  []interface{} `bson:",omitempty" json:"moderators"`  //userid
    Logo        string        `bson:",omitempty" json:"logo"`        // relative path to file
    Stylesheets []string      `bson:",omitempty" json:"stylesheets"` // absolute path to files
    Javascripts []string      `bson:",omitempty" json:"javascripts"` // absolute path to files
    Categories  []*Category   `json:"categories"`
}

Category struct {
    Id          bson.ObjectId `bson:"_id,omitempty" json:"id"`
    Name        string        `json:"name"`
    Slug        string        `json:"slug"`
    AdminOnly   bool          `json:"-"`
    MembersOnly bool          `json:"-"`
    Forums      []*Forum      `json:"forums"`
}

Forum struct {
    Id         bson.ObjectId `bson:"_id,omitempty" json:"id"`
    Name       string        `json:"name"`
    Slug       string        `json:"slug"`
    Text       string        `json:"text"`
    Moderators []interface{} `bson:",omitempty" json:"moderators"` //userid
}
4

2 に答える 2

0

(上記のように)これは機能しますが:

err := collection.Find(nil).Select(bson.M{"categories": bson.M{"$elemMatch": bson.M{"slug": "general"}}}).One(&result)

しかし、これはコレクション内のすべてのドキュメントを照会し、正しいドキュメントを選択すると思います。したがって、以下のソリューションがより効率的だと思います。

err := collection.Find(bson.M{"categories": bson.M{"$elemMatch": bson.M{"slug": "general"}}}).One(&result)
于 2017-07-02T11:27:37.110 に答える