2

My model looks like this:

Collection = new Schema({
 name: {type: String, required: true},
 tags: [String],
 movies: [{_id: false,
          original_title: {type: String}}]
)};

I need to alter the following query to use 'req.body' (an array of strings) to not only find a match in the 'tags' array but also find match(es) with the 'original_title' field in the movies array.

Collection.find({'tags' : { $in : req.body}}, (err, collections) => {           
            if(err){res.status(400).json(err);}

            if(!collections)
            {
                res.status(404).json({message: 'No collections found'});
            }else{
                res.json(collections);
            }
        }).limit(8);

1 Answer 1

3

Try this:

db.collection.find({
    $or: [{
            'tags': {
                $in: req.body
            }
        },
        {
            'original_title': {
                $in: req.body
            }
        }
    ]
}, (err, collections) => {
    if (err) {
        res.status(400).json(err);
    }

    if (!collections) {
        res.status(404).json({
            message: 'No collections found'
        });
    } else {
        res.json(collections);
    }
}).limit(8);
Sign up to request clarification or add additional context in comments.

2 Comments

This works, but I'm having problems with case sensitivity. Any idea how to ignore that for the 'original_title' condition?
I know it can be done for a single string using regex, not sure about an array. But I still think it's better for you to store and search your values when they are all consistently lower or upper cased.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.