Yongky Ali
Yongky Ali

Reputation: 492

Mongoose - Is it possible to modify key's value to match with where condition for find() query?

I am very familiar with Oracle when suddenly i had to create a project with MongoDB database. I am using mongoose to connect to my MongoDB. My question is, is that possible to match find condition before doing a query? For example, i got this name : John in my MongoDB then i just need to do this by Oracle SQL.

SELECT * FROM MY_SCHEMA.WORKERS WORKER
WHERE UPPER(WORKER.NAME) = 'JOHN'

In mongoDB, all I know and can do is this.

var mongoose = require('mongoose');
mongoose.connect('localhost:27017/myDB');
workers.find(
  {
    "name": "JOHN"
  }
);

I have been searching for solution and can't find one. Is there any way to set name's value to upper case to match the result i needed?

Any help would be appreciated :)

EDIT : I found the solution to this problem by using RegExp() for mongoose. RegExp will make my mongoDB docs case-insensitive to match my query's parameter. More details in this answer. https://stackoverflow.com/a/9824712/7498283

Upvotes: 1

Views: 272

Answers (2)

Ayush Mittal
Ayush Mittal

Reputation: 1

You can use $toUpper operator in mongodb.

    worker.aggregate([
           {$project: {"name":{$toUpper:"$name"}}},
           {$find:{"name":"JOHN"}}
    ]);

Upvotes: 0

Shrabanee
Shrabanee

Reputation: 2766

You can use toUpperCase() of javascript, if you want to do find with uppercase letters. refer-doc.

Try the code below:

workers.find(
{
  "name": ("JOHN").toUpperCase()
});

Hope this will help.

Upvotes: 1

Related Questions