Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NestJS Mongoose populate() Not Working for Arrays of ObjectIds, But Works for Single ObjectId #13497

Closed
3 of 15 tasks
alexpiczenik opened this issue Apr 27, 2024 · 4 comments
Closed
3 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@alexpiczenik
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Context
I am developing an application using NestJS and Mongoose and have encountered a problem when using the populate() method in a schema that contains an array of ObjectIds. The method works correctly when the field is a single ObjectId, but fails to populate properly when the field is an array of ObjectIds.

Models and Schemas
Here are the Mongoose schemas for User and Card:

User Schema
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Types, Document } from 'mongoose';

@Schema()
export class UserClass {
  @Prop({ required: true, unique: true })
  username: string;

  @Prop({ type: [{ type: Types.ObjectId, ref: 'Card' }] })
  cards: Types.ObjectId[];
}

export const UserEntity = SchemaFactory.createForClass(UserClass);
Card Schema

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';

@Schema()
export class CardClass {
  @Prop({ required: true, unique: true })
  token: string;

  // Additional properties
}

export const CardEntity = SchemaFactory.createForClass(CardClass);

Repository Method
Here is the method in my repository where I attempt to use populate():


import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User, UserDocument } from './user.entity';

@Injectable()
export class UsersRepository {
  constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) {}

  async findUserWithCardsById(userId: string): Promise<UserDocument | null> {
    return this.userModel
      .findOne({ _id: userId })
      .populate('cards')
      .exec();
  }
}

Issue
When executing the findUserWithCardsById method, the cards field should be populated with the Card documents corresponding to the IDs stored in the user's cards array. However, the array returns unpopulated, although it does work when the cards field is a single ObjectId (non-array).

Additional Context
Interestingly, when I modify the cards field in the User schema from an array to a single ObjectId:


@Prop({ type: Types.ObjectId, ref: 'Card' })
cards: Types.ObjectId;

The populate() function works perfectly, returning the expected Card attributes. This confirms that the collections and models are correctly connected and referenced, as the population performs correctly when cards is configured as a single ObjectId. This indicates that the issue is specific to handling arrays of ObjectIds.

Question
How can I resolve this issue so that populate() functions correctly with arrays of ObjectIds in NestJS with Mongoose? Is there any additional configuration or step I need to implement to handle arrays of references properly?

Acknowledgements
I would appreciate any help or guidance on how to solve this issue, as it is crucial for the functionality of my application.

Minimum reproduction code

--

Steps to reproduce

No response

Expected behavior

Expected Behavior
When using Mongoose's populate() method with an array of ObjectIds in the User schema, I expect that each ObjectId in the cards array should be populated with the corresponding Card document from the database. This should result in the cards array in the User document containing full Card document details for each reference, not just the ObjectIds.

Here is the code snippet of what I expect to happen:


// Repository method expected to populate user's cards
async findUserWithCardsById(userId: string): Promise<UserDocument | null> {
    return this.userModel
      .findOne({ _id: userId })
      .populate({
        path: 'cards',
        select: 'token subtoken franchise lastDigits validUntil'  // Expected fields to be populated from Card
      })
      .exec();
}

// Expected result structure
{
  _id: userId,
  username: 'johndoe',
  cards: [
    {
      _id: cardId,
      token: 'someToken',
      subtoken: 'someSubtoken',
      franchise: 'someFranchise',
      lastDigits: '1234',
      validUntil: '12/2030'
    },
    // Additional cards populated similarly
  ]
}

This structured result is what I aim to achieve, where each card's detailed information is retrieved and shown as part of the user's document. This functionality works as expected when cards is a single ObjectId but not when it is an array of ObjectIds, despite the collections and models being correctly connected and referenced.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

No response

Packages versions

[System Information]
OS Version     : macOS 22.1.0
NodeJS Version : v18.15.0
NPM Version    : 9.5.0 

[Nest CLI]
Nest CLI Version : 10.3.2 

[Nest Platform Information]
platform-express version : 10.3.3
mapped-types version     : 2.0.5
schematics version       : 10.1.1
mongoose version         : 10.0.4
passport version         : 10.0.3
testing version          : 10.3.3
common version           : 10.3.3
config version           : 3.2.0
core version             : 10.3.3
jwt version              : 10.2.0
cli version              : 10.3.2
 version                 : 1.11.2


Node.js version

v18.15.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@alexpiczenik alexpiczenik added the needs triage This issue has not been looked into label Apr 27, 2024
@micalevisk
Copy link
Member

I believe that this is not an issue on nestjs side. If you think otherwise, please share a minimum reproduction repository

@alexpiczenik
Copy link
Author

Thank you for your response. I appreciate your insight, and I agree that this might not be an issue with NestJS itself but rather with how Mongoose is handling the population of arrays of ObjectIds. However, given that this functionality is crucial to the application I am developing, I would greatly appreciate any further assistance. If anyone has encountered and resolved a similar issue or has suggestions on potential workarounds, it would be immensely helpful.

@gterras
Copy link

gterras commented Apr 30, 2024

As per the docs:

In case there are multiple owners, your property configuration should look as follows:
@prop({ type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Owner' }] })
owner: Owner[];

As written in the issue form when in doubt go and check the discord where people can help sorting implementation issues from bugs.

@kamilmysliwiec
Copy link
Member

Please, report this issue in the mongoose project.

@nestjs nestjs locked and limited conversation to collaborators Apr 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

4 participants