Andon Mitev
Andon Mitev

Reputation: 1494

Typeorm, MongoDB and where operator

[TypeORM with MongoDB type]. Hello I have db with users where each user have a email prop, now I want to find all users on which email starts with "test" for example and return value should be User[{email: 'test1'}, {email: 'test2'}, ...] but I'm not able to pull these entities here how i do my query:

 const users = await this.usersRepository.find({
    where: `"email" LIKE 'test%'`,
 });

But this code returns the whole collection of users. I also try it with:

I also try it with:

    const users = await this.usersRepository.find({
        where: {
            email: Like('test')
        }
    });

This one return empty array []

Even if I try with exact match for example Like('[email protected]'), still returns empty array

How I can achieve to get all users on which email starts with 'test' thanks in advance!

Upvotes: 1

Views: 3894

Answers (1)

Van SallyOne
Van SallyOne

Reputation: 993

For MongoDB use regex

const users = await this.usersRepository.find({
    where: {
        email: new RegExp(`^${userEmail}`);
    }
});

Upvotes: 4

Related Questions