score:1
Accepted answer
Use the RegExp object constructor to create a regular expression from a string. You need to first update the string to include pipes, i.e. create a regex match that replaces comma with a pipe and then in your find query use one regex, not an array of them, like [/a|b|c/i]
.
To illustrate the above with mongo shell:
// insert test documents
db.product.insert({x: "A"});
db.product.insert({x: "b"});
db.product.insert({x: "c"});
db.product.insert({x: "d"});
db.product.insert({x: "e"});
var brands = "a,b,c";
var pipe = brands.replace(/[ ]*,[ ]*|[ ]+/g, "|") // same as var pipe = "a|b|c";
var re = new RegExp(pipe, "i"); // same as var re = /a|b|c/i;
db.product.find({"x": {"$in": [re]}});
Returns:
/* 0 */
{
"_id" : ObjectId("552b7c869c752469483ce9c2"),
"x" : "A"
}
/* 1 */
{
"_id" : ObjectId("552b7c869c752469483ce9c3"),
"x" : "b"
}
/* 2 */
{
"_id" : ObjectId("552b7c869c752469483ce9c4"),
"x" : "c"
}
So you final code should look like:
var pipe = req.query['Brand'].replace(/[ ]*,[ ]*|[ ]+/g, "|");
var re = new RegExp(pipe, "i");
query['genetal.brand'] = {"$in": [re] };
More questions
- Case insensitive filter using mongodb $in
- MongoDB c# driver: Case Insensitive Compare using in or contains on a list using linq
- Case insensitive sorting in MongoDB
- MongoDB and C#: Case insensitive search
- Querying MongoDB (via pymongo) in case insensitive efficiently
- Query with filter builder on nested array using MongoDB C# driver
- Filter only by Date using mongoDB c# driver
- Mongodb sort with case insensitive manner
- MongoDB case insensitive index "starts with" performance problems
- Manually supplying arguments to a MongoDB query to support collation feature (for case insensitive index)
- MongoDB case insensitive key search
- MongoDB C# Case Insensitive Sort and Index
- MongoDB case insensitive query on text with parenthesis
- How can I replace an object in mongodb without specifying a filter using the C# driver?
- Better way to filter documents on mongodb using properties from another collection
- Mongodb fetch record in sub document using filter
- case insensitive find in mongodb for usernames in php
- MongoDB follow an index from a collection via regex case insensitive
- Mongodb C# FindAsync. Filter on list inside document using linq
- Nodejs - MongoDB findone with exact match but case insensitive
- how to make case insensitive query using variable?
- F# MongoDB using find with string filter
- Using $match to filter by dates using PHP MongoDB Aggregation Framework
- How to filter record from multilevel document as per user input using MongoDB and Node.js?
- How to apply filter on Array object in MongoDb table using Mongodb.Driver in C#?
- Exact search in mongodb 2.6, just only case insensitive
- Is a case insensitive aggregate match possible in Mongo without using $regex operators?
- How to filter a MongoDB collection using information from another collection?
- mongoDB query with case insensitive schema element
- mongodb query to filter the array of objects using $gte and $lte operator
More questions with similar tag
- Uploading multer files (nodejs) to Mongo database
- Mongoose how to use populate() when making aggregation with $facet?
- Import data to Docker-container's mongodb
- Spring MongoDB query documents if days difference is x days
- spring-data-mongodb aggregation with composite ID
- Retrieving, operating and storing date back in datetime
- Mongodb aggregate $match inconsistent handling of dates between mongo and javascript
- How can I pull sub-documents from an array?
- How to query element in array of arrays in MongoDB?
- Is BinData a Mongo ObjectId?
- How to change the mongod db query to aggregation
- Mongodb aggregation get documents by stringdate
- When I use angular 9 ngFor, the array does not show the corresponding data
- How to save multiple files in mongodb
- How to move embedded key-value pair one level up in mongodb
- Add a compound index when some fields can be null or arrays of strings
- Mongoose findOneAndUpdate + upsert always replaces existing document
- How to update an object and push into an array at the same time in mongoose
- Why i can't increment in mongoose / mongodb?
- mongodb array updates in python script
- $group and timestamp $sort mongodb
- how to update only one attribute in mongoose
- Loop through each collection in a database -- mongodb
- Impossible to get property from mongoose object
- How does mongodb compare/sort by _id?
- Changing order of object in object array with an element key in mongo collection object
- Configuring QueryDSL for Spring Data and MongoDB with Gradle
- update mongoDB from vb.net
- Initializing Gridfs returns TypeError: string is not a function
- How to check whether a certain field is indexed in mongodb or not?
Source:
stackoverflow.com