sql >> データベース >  >> NoSQL >> MongoDB

ラムダ関数を使用してmongodbの2つのコレクション間で複数の結合を行う方法は?

    これを確認してください:

    db.collection("user_posts").aggregate(
    { $match: {"userid" : uid}},
    { $unwind: '$like' },
    { $lookup: { from: "users", localField: "like.userid", foreignField: "_id", as: 
    "users" }},
    { $group: {
        _id: "$_id",
        like: { $push: { $mergeObjects: ['$like', { $arrayElemAt: [ "$users", 0 ] } ]}},
        data: { $first: "$$ROOT" }
    }},
    { $replaceRoot: { newRoot: { $mergeObjects: ['$data', { like: "$like"} ]} } },
    { $unwind: '$comment' },
    { $lookup: { from: "users", localField: "comment.userid", foreignField: "_id", as: 
     "users" }},
    { $group: {
        _id: "$_id",
            comment: { $push: { $mergeObjects: ['$comment', { $arrayElemAt: [ "$users", 0 
     ] } ]}},
            data: { $first: "$$ROOT" }
    }},
    { $replaceRoot: { newRoot: { $mergeObjects: ['$data', { comment: "$comment"} ]} } },
    { $unwind: '$share' },
    { $lookup: { from: "users", localField: "share.userid", foreignField: "_id", as: 
    "users" }},
    { $group: {
        _id: "$_id",
        share: { $push: { $mergeObjects: ['$share', { $arrayElemAt: [ "$users", 0 ] } 
    ]}},
        data: { $first: "$$ROOT" }
    }},
    { $replaceRoot: { newRoot: { $mergeObjects: ['$data', { share: "$share"} ]} } },
    { $project: { users: 0 }}
    )
    

    要件に応じて、出力を取得し、プロジェクト集約のフィールド名を追加/削除します



    1. MongoDB C#ドライバーで生成されたスクリプトを取得します

    2. MongoDBクエリ:$ near withgregation

    3. MongoDB Atlasエラー:スキーマが無効です。mongodbが必要です

    4. WindowsDockermongoコンテナーはボリュームマウントでは機能しません