user_agent
user_agent

Reputation: 65

Can not search from mongodb database as per letter using Node.js and mongoDB

I have one search functionality where when user user will type the letter it will search with mongoDB document but as per my code its only searching with some specific letter. I am explaining my code below.

Document::

{ "_id" : 1, "StoreCode" : "DKSR", "IsDeleted" : false }
{ "_id" : 2, "StoreCode" : "DKJG", "IsDeleted" : false }

MongoDB query::

search='dksr';

const aggregatePipeLine = [     
            {
                $match: {
                    IsDeleted: false,
                    StoreCode: { $regex : new RegExp(search)}
                }
            },
            {
                $lookup: {
                    from: "customers",
                    localField: "CustomerId",
                    foreignField: "_id",
                    as: "CustInfo"
                }
            }
        ];

aggregatePipeLine.push({$sort: { UpdatedAt : -1 }});
let CustomerGroup = await CustomerGroupCollection.aggregate(aggregatePipeLine).limit(limit).skip(skip);

Here my issue is when search='DKSR' its working as expected but if user is typing search='dksR' no record is fetching. I need for each letter (May be capital or small) it should give the appropriate result.

Upvotes: 1

Views: 85

Answers (1)

mickl
mickl

Reputation: 49985

You can use $options to perform case-insensitive search:

{ StoreCode: { $regex: new RegExp('dksR'), $options: 'i' } }

Upvotes: 1

Related Questions