简体   繁体   中英

Express.js - Mongoose multiple or conditions

I have a userSchema i have to search in the user records with a search key word

//userSchema 
const userSchema = new Schema(
    {
        name: String,
        city: String,
        postalCode: Number
    },
    {
        timestamps: true
    }
);

So the API route is localhost:3000/api/v1/search/:key the key can be user name or city or postalCode, based on the key we have to get the results from collection.

this is what i have tried userController

index: async (req, res, next) => {

    const searchKey = req.params.key;

    const searchResult = await User.find({
        $or:[{name: searchKey},{city: searchKey},{postalCode: searchKey}]
    });

    res.status(200).json({
        message: 'query was succesfull',
        data: searchResult
    });
}

if i pass :key as Canada i am getting following error. if i pass :key as 123456 which is postalCode i am getting data

{
    "error": {
        "message": "Cast to number failed for value \"Canada\" at path \"postalCode\" for model \"users\""
    }
}

So how do i make this query more robust so that based on the :key i have to get the data from user model

Looking for much needed help

Thank you.

Because in the userSchema you declared postalCode as type of number, mongoose expect the searchKey to be number, try to build the query condition based on searchKey type:

// if searchKey is number
if ( parseInt(searchKey) ) {
    var condition = { postalCode: searchKey };
} else {
    var condition = { $or:[ { name: searchKey }, { city: searchKey }] };
}

const searchResult = await User.find(condition);

Edit: Use $regex operator if you want to search by Word Similarities, but first you need to change postalCode type to string:

const searchResult = await User.find({
    $or:[ 
        { name: { $regex: new RegExp(searchKey, 'i') } },
        { city: { $regex: new RegExp(searchKey, 'i') } },
        { postalCode: { $regex: new RegExp(searchKey, 'i') } }
    ]
});

尝试:

{postalCode: isNaN(parseInt(searchKey)) ? undefined : parseInt(searchKey)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM