1
//PhoneProduct is model
//My schema has brand field with string type
var brands = ['apple', 'samsung'];
var QueryResult = PhoneProduct.find().where('brand').equals(brands));

Is there any way I get all document that have brand equal to any element in brands array ?

2
  • Well $in of course. Mongoose even has a helper QueryResult = PhoneProduct.find().where('brand').in(brands) Commented Jun 15, 2017 at 7:29
  • Thank you. Your code works. I've tested my above code and it also work. Commented Jun 15, 2017 at 8:12

3 Answers 3

1
var QueryResult = PhoneProduct.find({'brand': {$in: brands }})
Sign up to request clarification or add additional context in comments.

Comments

1

You can not campare string with array, for this you need to check it belongs from array or not, Try This

//PhoneProduct is model
//My schema has brand field with string type
var brands = ['apple', 'samsung'];
var QueryResult = PhoneProduct.find({brand: {$in: brands}})

Comments

0

You can compare with for (i) meaning a loop:

var myobj0 = { symbols: { $in: ["#" + "AVAX"] } }
find('XDB, 'XCOLLECTION', myobj0).then(result => {
    console.log("These are here", result)
    for (i = 0; i < result.length; i++) {
        console.log("IDS:" + result[i].id)
        bot.sendMessage(result[i].id, message);
    }
})

You can also define the strength to match (as in Regex, but more efficient, since MongoDB built in)

const find = async (dbName, collectionName, json) => {
    try {
        const collection = client.db(dbName).collection(collectionName);
        const result = await collection.find(json).collation( { locale: 'en', strength: 2 }).toArray();
        return result;
    } catch (err) {
        console.error(err);
    }
}

Maybe you just want to compare some asset that is already built in too. Please check collations here on Mongo official manual

Comments

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.