Ways to convert field data to lowercase in MongoDB query result

I have two Objects in my database collection.

[
  {"_id" : 1, name: "notLowercased"},
  {"_id" : 2, name: "lowercased"},
]

Using find and $regex to search for names that include a specific string.

data = await CatalogModel.find({name: {$regex : searcher.toString().toLowerCase()}})

For example, if the input string is "lowercased".

The result returns an array:

[
  {"_id" : 2, name: "lowercased"},
]

However, I want the result to be:

[
  {"_id" : 1, name: "notLowercased"},
  {"_id" : 2, name: "lowercased"},
]

I understand that this happens because "notLowercased" is not lowercased.

How can I lowercase the name fields in this request?

Answer №1

You have the option to include the $options parameter in this manner: $options: "i".

As stated in the documentation:

i: Enables case insensitivity, allowing matches regardless of letter case. Refer to Perform Case-Insensitive Regular Expression Match for an example.

Furthermore, you can circumvent the use of toLowerCase()

data = await CatalogModel.find({name: {$regex : searcher.toString(), "$options": "i" }})

See an example with and another without using toLowerCase() here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Node.js, sigma.js, and the typescript environment do not have a defined window

When attempting to set up a sigma.js project with node.js in TypeScript, I encountered a reference error after starting the node.js server: ts-node index.ts The problem seems to be located within the sigma\utils\index.js file. <nodejsproject& ...

Determining if a map array value is being duplicated with a distinct key in JavaScript

I am facing an issue with a Map that has "String" as keys and "Array" as values. My problem is figuring out how to check if an array item is present in a different "Array" value, specifically in the "Array" of a different key within the map. For example: ...

Mongoose: issues with the populate method implementation

I'm in need of some assistance here, as I'm unable to determine why the populate method is not working as expected. My goal is to create a model for "rooms" with people inside them. var PersonSchema = new Schema({ _id : Schema.ObjectId, ...

Step-by-step guide on generating migrations using moongoose

When it comes to node js, I am accustomed to working with knex for my applications. Knex allows us to create migrations that define the table structures at the start. Does mongoose have a similar feature for creating schemas and seeding initial data? ...

Implementing 'Load more...' in Rails for infinite scrolling instead of traditional pagination

I currently have a collection of items and have been utilizing the will_paginate gem. However, I am interested in implementing a "load more..." feature at the end of the list. Is there a straightforward way to achieve this with will_paginate, or should I e ...

What is the best way to implement a response.write function within the request module?

I have a project where I am setting up an HTTP server and sending a request to the Yahoo Finance website to retrieve some data. My goal is to display this data on the browser, but I'm facing an issue with the response.write function not working inside ...

What's the best way to interact with Vuex: accessing the store directly or passing properties

I am currently in the process of transitioning a pure Vue application to Vuex, and I'm grappling with the most efficient method of passing data down to the components. In Vue, it is common practice to pass data as properties like this: <component ...

Having difficulty accessing `props` in a React JS and Next JS application

Currently, I am developing a React application that utilizes Server Side Rendering. In this project, I am using React Js and Next Js as my primary framework. My goal is to retrieve initial props using the getServerSideProps method by consulting the documen ...

The quantity of elements stays constant even after adding to them using Javascript

I have implemented a notes list feature that allows users to add notes using an input field. Beneath the notes list ul, there is a message showing the total number of notes: $('li').length. However, I encountered an issue where the count of not ...

Moving the Promise.all feature from AngularJs to VueJs

I'm currently facing a challenge with moving a function from AngularJs to VueJs. I would really appreciate any help or suggestions you may have! items = { one: {...details here...}, two: {}, } In AngularJs: var promises = []; var deferred = $ ...

Updating the state in a React array of objects can be achieved by checking if the specific id already exists in the array. If

In the scenario where the parent component receives an object from the child component via a callback function, I need to verify the existence of an object with a specific rowID. If it exists, the object should be updated with the passed value "val"; oth ...

How can I determine if my clients are utilizing the CDN or NPM versions of my JavaScript library?

At this moment, I'm contemplating releasing an open-source version of my library on NPM. My main concern is figuring out how to track the usage of my CDN or NPM by clients. Is there a method available to achieve this? ...

Is it possible to add a jQuery-generated element to pure vanilla JavaScript?

I am facing a challenge in creating a new notification div when an event is triggered. Ideally, I would normally achieve this using jQuery by utilizing something like $("myDiv").append(newDiv). However, in this case, the item selector to which the new div ...

When the HTML content matches a specific value, initiate a click event to trigger

Can anyone help me troubleshoot? I've tried multiple methods but can't seem to get it right. Here's a breakdown of what I'm attempting to accomplish: #info-NUMBER-btn shows Click to display more information. #info-NUMBER CSS is set t ...

I must interact with the video within the iframe by clicking on it

I am trying to interact with an iframe video on a webpage. Here is the code snippet for the video: <div class="videoWrapper" style="" xpath="1"> <iframe width="854" height="480" src="xxxxxxx" frameborder="0" allow="autoplay; encrypted-media" all ...

React-router-sitemap lacks a definition for 'Require'

According to the official documentation here, this is how the sitemap-builder.js file should be structured: require('babel-register'); const router = require('./router').default; const Sitemap = require('../').default; ( ...

The combination of loading and scrolling JavaScript code is not functioning properly on the website

I created an HTML webpage that includes some JavaScript code to enhance the user experience. However, I encountered an issue when trying to incorporate a load JavaScript function alongside the scroll JavaScript function on my page. The load script is posi ...

Make sure to wait for the stored procedure to finish executing before running the Python script in Node/Express using Tedious

I'm struggling to figure out how to properly time the execution of a python script following the completion of a SQL Server stored procedure via a router.post (express4/tedious): router.post('/post/create', textParser, function (req, res) { ...

I'm curious about why I'm receiving the error "Unable to bind to 'ngFor' since it is not recognized as a property of 'li'. Can someone please explain why this is happening?

The issue is related to the *ngFor directive for nonvegfoodlist app.component.ts import { Component } from '@angular/core'; export class Menu { id : number; name :string; } const veg : Menu[] = [ { id:1 , name:'Rice'}, { id: ...

Exploring the world of jQuery AJAX alongside Google's currency calculator

I'm currently working on a code that utilizes an AJAX call to access the Google currency calculator. The expected outcome is to receive a JSON array that can then be used to gather exchange rate data. Here is the link: http://www.google.com/ig/cal ...