Bird
0
0

What will the following QueryBuilder code return?

medium📝 component behavior Q4 of 15
NestJS - Database with TypeORM
What will the following QueryBuilder code return?
const users = await this.userRepository.createQueryBuilder('user')
  .select(['user.id', 'user.name'])
  .where('user.isActive = :active', { active: true })
  .getMany();
AAn error because select needs all fields
BAn array of all user objects ignoring isActive
CA single user object with all fields
DAn array of user objects with only id and name where isActive is true
Step-by-Step Solution
Solution:
  1. Step 1: Analyze select and where clauses

    The select limits fields to id and name. The where filters users with isActive true.
  2. Step 2: Understand getMany() behavior

    getMany() returns an array of matching entities with selected fields.
  3. Final Answer:

    An array of user objects with only id and name where isActive is true -> Option D
  4. Quick Check:

    getMany() returns filtered array with selected fields [OK]
Quick Trick: getMany() returns array; select limits fields [OK]
Common Mistakes:
  • Thinking getMany() returns a single object
  • Assuming select must include all fields
  • Ignoring the where filter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes