0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【MongoDB】lookupで結合した先のフィールド名にエイリアスを設定したい

Posted at

概要

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)),
        )
    )
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?