私はマングースに不慣れで、以下の2つのスキーマがあります
contentschema
{ "_id" : autogenerated
"title": "string",
"description": "string",
}
viewedschema
{ "_id" : autogenerated
"contentid": "ref content",
"viewedby": "string",
}
コンテンツを表示したすべてのユーザーは、contentid参照を持つviewedschemaコレクションに保存されます。
注:表示されるレコードの数が膨大になるため、コンテンツ内で埋め込みドキュメントとして表示したくありません。
マングースでは、すべてのコンテンツ(コンテンツスキーマの配列)を表示する方法はありますか?[SQLの内部結合に似ています]。
前もって感謝します。
$ lookupを使用して、同じデータベースの2つの異なるコレクション内のドキュメントをマージし、コレクションに対して左外部結合を実行できます。
以下の資料をcontent
集めてみましょう
{
"_id" : ObjectId("59ef51f106b0505f997f84c8"),
"title" : "myfavoritesong",
"description" : "A wonderful composition using string instruments"
}
{
"_id" : ObjectId("59ef52ad06b0505f997f84ca"),
"title" : "myfavoritestory",
"description" : "An interesting short story with a twisted ending"
}
viewed
コレクション内のドキュメント
{
"_id" : ObjectId("59ef523706b0505f997f84c9"),
"contentid" : ObjectId("59ef51f106b0505f997f84c8"),
"viewedby" : "user1"
}
{
"_id" : ObjectId("59ef52f406b0505f997f84cb"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user2"
}
{
"_id" : ObjectId("59ef53c706b0505f997f84cc"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user3"
}
2つのコレクションを組み合わせて$ lookupを使用した最終的な集計クエリは次のとおりです。
db.viewed.aggregate({
$lookup:{
from : "content",
localField: "contentid",
foreignField:"_id",
as:"viewed_contents"
}
})
サンプルデータの集計クエリの結果は次のとおりです。
{
"_id" : ObjectId("59ef523706b0505f997f84c9"),
"contentid" : ObjectId("59ef51f106b0505f997f84c8"),
"viewedby" : "user1",
"viewed_contents" : [
{
"_id" : ObjectId("59ef51f106b0505f997f84c8"),
"title" : "myfavoritesong",
"description" : "A wonderful composition using string in
struments"
}
]
}
{
"_id" : ObjectId("59ef52f406b0505f997f84cb"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user2",
"viewed_contents" : [
{
"_id" : ObjectId("59ef52ad06b0505f997f84ca"),
"title" : "myfavoritestory",
"description" : "An interesting short story with a twist
ed ending"
}
]
}
{
"_id" : ObjectId("59ef53c706b0505f997f84cc"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user3",
"viewed_contents" : [
{
"_id" : ObjectId("59ef52ad06b0505f997f84ca"),
"title" : "myfavoritestory",
"description" : "An interesting short story with a twist
ed ending"
}
]
}
viewed
外国とcontent
地元のコレクションを交換することもできますのでご注意ください
db.content.aggregate({
$lookup:{
from : "viewed",
localField: "_id",
foreignField:"contentid",
as:"contents_viewed_by"
}
})
この集計クエリの結果は次のとおりです
{
"_id" : ObjectId("59ef51f106b0505f997f84c8"),
"title" : "myfavoritesong",
"description" : "A wonderful composition using string instruments",
"contents_viewed_by" : [
{
"_id" : ObjectId("59ef523706b0505f997f84c9"),
"contentid" : ObjectId("59ef51f106b0505f997f84c8"),
"viewedby" : "user1"
}
]
}
{
"_id" : ObjectId("59ef52ad06b0505f997f84ca"),
"title" : "myfavoritestory",
"description" : "An interesting short story with a twisted ending",
"contents_viewed_by" : [
{
"_id" : ObjectId("59ef52f406b0505f997f84cb"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user2"
},
{
"_id" : ObjectId("59ef53c706b0505f997f84cc"),
"contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
"viewedby" : "user3"
}
]
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加