概要
MongoDBではAggregationのクエリでlookupを使用することで、他コレクションと結合できるのですが、O/Rマッパーを使用する時など取得時のフィールド名を調整したい場合があります。
今回はこういったケースの場合に、どうクエリを書けば良いかのメモ書きです。
前提
- 使用したMongoDBのバージョンは
7.0
です。
対応方針
Mongodb creating alias in a queryのstackoverflowの回答にある通り、project句の中でlastName: "$author.last"
のような形でエイリアスが設定できます。結合先のコレクション名を参照し、これにエイリアスを設定すれば実現できそうです。
実装サンプル
以下の例では、Pythonのmongoengineを使用してクエリを実装しています。
post_categories
コレクションのcreate_user_account_id
と、user_accounts
コレクションの_id
を結合させます。
そして、user_accounts
コレクションのname
フィールドのエイリアスをuser_name
とします。
def find_post_categories() -> List[PostCategoryQueryResult]:
pipeline = [
{
"$lookup": {
"from": "user_accounts",
"localField": "create_user_account_id",
"foreignField": "_id",
"as": "user_accounts",
}
},
{"$unwind": "$user_accounts"},
{
"$project": {
"_id": 1,
"name": 1,
"user_name": "$user_accounts.name",
"parent_category_id": 1,
"memo": 1,
}
},
]
# クエリ結果のdictをクラスのインスタンスに変換
return list(
map(
lambda c_dict: PostCategoryQueryResult(**c_dict),
list(PostCategory.objects().aggregate(pipeline)),
)
)