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

ネストされた配列にforeignFieldがある場合の$lookup

    あなたの質問を完全に理解できるかどうかはわかりませんが、これはあなたに役立つはずです:

    db.student.aggregate([{
        $match: { _id: ObjectId("657...") }
    }, {
        $lookup: {
            from: 'library',
            localField: '_id' ,
            foreignField: 'issued_to.student',
            as: 'result'
        }
    }])
    

    すべてのbook_nameのみを取得したい場合 ■生徒ごとにこれを行うことができます:

    db.student.aggregate([{
        $match: { _id: ObjectId("657657657657657657657657") }
    }, {
        $lookup: {
            from: 'library',
            let: { 'stu_id': '$_id' },
            pipeline: [{
                $unwind: '$issued_to' // $expr cannot digest arrays so we need to unwind which hurts performance...
            }, {
                $match: { $expr: { $eq: [ '$issued_to.student', '$$stu_id' ] } }
            }, {
                $project: { _id: 0, "book_name": 1 } // only include the book_name field
            }],
            as: 'result'
        }
    }])
    


    1. マングースに埋め込まれたスキームドキュメントを削除するにはどうすればよいですか?

    2. モンゴッドとモンゴスの正確な違いは何ですか

    3. MongoDBシャードクラスターのトラブルシューティング

    4. なぜMongodbのパフォーマンスがWindowsよりもLinuxの方が優れているのですか?