title: NestJS and Mongoose find by reference object Id
body: <p>I have a Mongo collection of Users and a collection of Addresses. Each address is owned by one user.Here are my schema classes:</p>
  <pre><code>
  export type UserDocument = User &amp; mongoose.Document
  @Schema({ timestamps: true })
  export class User {
    // @Prop({ type: mongoose.Types.ObjectId })
    _id: string
  
    @Prop({ required: true })
    name: string
  
    @Prop({ required: true, unique: true })
    email: string
  
    @Prop({ select: false })
    password: string
  
  }
  export const UserSchema = SchemaFactory.createForClass(User)
  
  export type AddressDocument = Address &amp; Document
  @Schema({ timestamps: true })
  export class Address {
    @Prop({ type: mongoose.Schema.Types.ObjectId, ref: User.name })
    user: User
  
    @Prop()
    line1: string
  
    @Prop()
    line2?: string
  
    @Prop()
    city: string
  
    @Prop()
    state?: string
  
    @Prop()
    country: string
  }
  
  export const AddressSchema = SchemaFactory.createForClass(Address)
  </code></pre>
  <p>Then I have an AddressService that can fetch addresses for a user:</p>
  <pre><code>@Injectable()
  export class AddressService {
    constructor(@InjectModel(Address.name) private addressModel: Model&lt;AddressDocument&gt;) {}
  
    async save(address: AddressDto): Promise&lt;Address&gt; {
      const model = await this.addressModel.create(address)
      return model.save()
    }
  
    async findAddressForUser(userId: string) {
      const userObjectId = new mongoose.Types.ObjectId(userId)
      const users = await this.addressModel.find({ user: userObjectId })
    }
  }
  </code></pre>
  <p>This code has an error: <code>Type '_ObjectId' is not assignable to type 'Condition&lt;User&gt;</code>
  I tried passing the userId as a string as well, that did not work either.</p>
  <p>What is the right way to query a collection using a reference Id from another collection?</p>