Find document with array that contains a specific value

前端 未结 10 1480
猫巷女王i
猫巷女王i 2020-11-22 04:06

If I have this schema...

person = {
    name : String,
    favoriteFoods : Array
}

... where the favoriteFoods array is popula

10条回答
  •  生来不讨喜
    2020-11-22 04:26

    If you'd want to use something like a "contains" operator through javascript, you can always use a Regular expression for that...

    eg. Say you want to retrieve a customer having "Bartolomew" as name

    async function getBartolomew() {
        const custStartWith_Bart = await Customers.find({name: /^Bart/ }); // Starts with Bart
        const custEndWith_lomew = await Customers.find({name: /lomew$/ }); // Ends with lomew
        const custContains_rtol = await Customers.find({name: /.*rtol.*/ }); // Contains rtol
    
        console.log(custStartWith_Bart);
        console.log(custEndWith_lomew);
        console.log(custContains_rtol);
    }
    

提交回复
热议问题