Find matching element from document based on regex value in mongidb

I need to select the matching casts

[ m_credits_cast

] element from the Movie collection using the Regex value.

Modal film

var mongoose = require('mongoose');
var movieSchema = new mongoose.Schema({
    m_tmdb_id: {
        type: Number,
        unique: true,
        index: true
    },
    m_original_title: {
        type: String
    },
    m_poster_path: {
        type: String
    },
    m_credits_cast: {
        type: Array
    },
    m_release_date: {
        type: Date
    },
    m_title: {
        type: String
    },
    created_at: {
        type: Date
    },
    updated_at: {
        type: Date,
        default: Date.now
    }
});
var MovieModel = mongoose.model('Movie', movieSchema);
module.exports = {
    movie: MovieModel
}

      

Js code

router.post('/casts', function(req, res, next) {
    var searchText = req.body.searchtext;
    var regexValue = '^' + searchText;
    Movie
        .aggregate([{
                $match: {
                    'm_credits_cast.name': { '$regex': regexValue, '$options': 'i' }
                }
            },
            {
                $project: {
                    m_credits_cast: {
                        $filter: {
                            input: '$m_credits_cast',
                            as: 'cast',
                            cond: { $eq: ['$$cast.name', { '$regex': regexValue, '$options': 'i' }] }
                        }
                    },
                    _id: 0
                }
            }
        ])
        .exec(function(err, casts) {
            return res.status(500).json(casts);
        }).catch(function(error) {
            return res.status(500).json(error);
        });
});

      

The code doesn't work because I am using $ regex inside the $ project statement . How can I solve this problem?

Film collection

[{
    "_id": "5913e96548f1956df16f33a0",
    "m_tmdb_id": 401246,
    "m_original_title": "The Square",
    "m_poster_path": "/g7ifEh3XBKftVA0g1zz9lXORm7l.jpg",
    "m_release_date": "2017-08-25T00:00:00.000Z",
    "m_runtime": 142,
    "m_credits_cast": [{
            "cast_id": 2,
            "character": "Christian",
            "credit_id": "575823d9c3a3687d6f00157f",
            "gender": 2,
            "id": 150802,
            "name": "Claes Bang",
            "order": 0,
            "profile_path": "/iwPu7hwSj6A9BkO9M13SDkhD7QE.jpg"
        },
        {
            "cast_id": 0,
            "character": "Anne",
            "credit_id": "575823bec3a3687d6a0016cf",
            "gender": 1,
            "id": 32798,
            "name": "Elisabeth Moss",
            "order": 1,
            "profile_path": "/iSp2t9GYUUgyqTpz25J6hOHH2An.jpg"
        }
    ],
    "m_genres": [{
        "id": 18,
        "name": "Drama"
    }]
}, {
    "_id": "5840ebceb386b78c69b56602",
    "m_tmdb_id": 336000,
    "m_original_title": "The Glass Castle",
    "m_poster_path": "/d5fLQ9ZMa1jZ2mODCv5lOWJDugX.jpg",
    "m_release_date": "2017-08-11T00:00:00.000Z",
    "m_runtime": 0,
    "m_credits_cast": [{
            "cast_id": 6,
            "character": "Jeannette Walls",
            "credit_id": "574fbcc6c3a3687950000f7c",
            "gender": 1,
            "id": 60073,
            "name": "Brie Larson",
            "order": 1,
            "profile_path": "/p66rOUPo1ZFZ7XrAKZ6DjdsvCA2.jpg"
        },
        {
            "cast_id": 7,
            "character": "Rose Mary Walls",
            "credit_id": "574fbcddc3a36879430011ff",
            "gender": 1,
            "id": 3489,
            "name": "Naomi Watts",
            "order": 2,
            "profile_path": "/8W02WOJI1pEGh2iqQsgITR5tV0P.jpg"
        },

        {
            "cast_id": 30,
            "character": "Miss Bivens",
            "credit_id": "58e3d04a925141281902086e",
            "gender": 1,
            "id": 43259,
            "name": "Kyra Harper",
            "order": 19,
            "profile_path": "/hItQl4CyIC6Www5FQeHVWrv76ap.jpg"
        },
        {
            "cast_id": 31,
            "character": "Jeannette Walls (Age 5 and 6)",
            "credit_id": "58e3d063c3a36872af01ecaf",
            "gender": 0,
            "id": 1505453,
            "name": "Chandler Head",
            "order": 20,
            "profile_path": null
        },
        {
            "cast_id": 32,
            "character": "Intern",
            "credit_id": "58e3d070925141281902089c",
            "gender": 0,
            "id": 1652138,
            "name": "Hamza Haq",
            "order": 21,
            "profile_path": null
        }
    ],
    "m_genres": [{
        "id": 18,
        "name": "Drama"
    }]
}]

      

+3


source to share





All Articles