如何在 nestjs 中填充猫鼬引用?

Yar*_*ron 7 mongoose mongoose-populate nestjs

我定义了一个人和故事模式:

    @Schema()
    export class Person extends Document {
      @Prop()
      name: string;
    }
    export const PersonSchema = SchemaFactory.createForClass(Person);
    
    
    @Schema()
    export class Story extends Document {
    
      @Prop()
      title: string;
    
      @Prop()
      author:  { type: MongooseSchema.Types.ObjectId , ref: 'Person' }
    
    }
    export const StorySchema = SchemaFactory.createForClass(Story);
Run Code Online (Sandbox Code Playgroud)

在我的服务中,我实现了保存和读取功能:

        async saveStory(){
        const newPerson = new this.personModel();
        newPerson.name  = 'Ian Fleming';
        await newPerson.save();
        const newStory  = new this.storyModel();
        newStory.title = 'Casino Royale';
        newStory.author = newPerson._id;
        await newStory.save();
      }
    
      async readStory(){
        const stories = await this.storyModel.
            findOne({ title: 'Casino Royale' })
        console.log('stories ',stories);
      }
Run Code Online (Sandbox Code Playgroud)

当我运行 readStory() 时,我得到以下输出:

     stories  {
      _id: 5f135150e46fa5256a3a1339,
      title: 'Casino Royale',
      author: 5f135150e46fa5256a3a1338,
      __v: 0
    }
Run Code Online (Sandbox Code Playgroud)

当我将 a 添加populate('author')到我的查询时,我将作者设为空:

     stories  {
      _id: 5f135150e46fa5256a3a1339,
      title: 'Casino Royale',
      author: null,
      __v: 0
    }
Run Code Online (Sandbox Code Playgroud)

如何使用引用的 Person 文档填充作者字段?

Yar*_*ron 7

找到了。我的错误在于定义模式。应该 :

@Schema()
export class Story extends Document {
  @Prop()
  title: string;
    
  @Prop({ type: MongooseSchema.Types.ObjectId , ref: 'Person' })
  author:  MongooseSchema.Types.ObjectId  
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你!在“Prop”选项中设置类型是我问题的解决方案,因为我使用联合类型来进行实际的打字稿类型。顺便说一句:您还可以 `import { Types } from 'mongoose';` 然后使用 `author: Types.ObjectId`。 (2认同)

Dic*_*Bos 6

在对 nestjs 中的猫鼬引用进行了大量阅读和测试之后。我认为可以改进接受的答案。我将分 2 个步骤展示这一点。第一步是显示 MongooseSchema 的声明,并包括@illnr 关于要使用的 author 属性Types.ObjectId而不是MongooseSchema.Types.ObjectId.

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

@Schema()
export class Story extends Document {

  @Prop()
  title: string;

  @Prop({ type: MongooseSchema.Types.ObjectId , ref: 'Person' })
  author:  Types.ObjectId 

}

export const StorySchema = SchemaFactory.createForClass(Story);
Run Code Online (Sandbox Code Playgroud)

作为第二步,我认为使用 Person 类作为 author 属性的类型可以提高可读性,如下所示。

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types, Schema as MongooseSchema } from 'mongoose';
import { Person } from './person.schema'

@Schema()
export class Story extends Document {

  @Prop()
  title: string;

  @Prop({ type: MongooseSchema.Types.ObjectId , ref: 'Person' })
  author:  Person

}

export const StorySchema = SchemaFactory.createForClass(Story);
Run Code Online (Sandbox Code Playgroud)