populate似乎在nestjs mongoose中不起作用

piwo6bdm  于 2024-01-08  发布在  Go
关注(0)|答案(1)|浏览(202)

我有一个服务方法,看起来像这样:

  1. async getTeamsByNightId(id: string) {
  2. const night = await this.nightModel.findById({_id: id});
  3. console.log('night: ', night);
  4. //@TODO Why is populate not working?
  5. //const night = (await this.nightModel.findById({_id: id})).populate('teams');
  6. if (night) {
  7. const teamIds = night.teams.map((teamId) => teamId.toString());
  8. console.log('teamIds: ', teamIds);
  9. try {
  10. const teams = await this.teamModel.find({ _id: { $in: night.teams } }).exec();
  11. console.log('teams: ', teams);
  12. } catch (error) {
  13. console.log(error);
  14. }
  15. } else {
  16. throw new NotFoundException('No matching Night found');
  17. }
  18. }

字符串
它正在产生这样的输出:

  1. Mongoose: nights.findOne({ _id: ObjectId("6552ad936a93bab2b686496d") }, {})
  2. night: {
  3. _id: new ObjectId("6552ad936a93bab2b686496d"),
  4. name: 'a',
  5. askedQuestions: [],
  6. teams: [ new ObjectId("6552ada76a93bab2b6864972") ],
  7. password: '487254',
  8. __v: 1
  9. }
  10. teamIds: [ '6552ada76a93bab2b6864972' ]
  11. Mongoose: teams.find({ _id: { '$in': [ ObjectId("6552ada76a93bab2b6864972") ] } }, {})
  12. teams: []


正如您所看到的,它在teams数组中有一个ObjectId,但是尝试在所有团队中查找它不会产生任何结果。
有以下模式:

  1. import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
  2. import { HydratedDocument, Types } from "mongoose";
  3. export type NightDocument = HydratedDocument<Night>;
  4. @Schema()
  5. export class Night {
  6. @Prop()
  7. name: string;
  8. @Prop()
  9. password: string;
  10. @Prop([{ type: Types.ObjectId, ref: 'Question' }])
  11. askedQuestions: Types.ObjectId[];
  12. @Prop([{ type: Types.ObjectId, ref: 'Team' }])
  13. teams: Types.ObjectId[];;
  14. }
  15. export const NightSchema = SchemaFactory.createForClass(Night);


和团队

  1. import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
  2. import { HydratedDocument, Types } from 'mongoose';
  3. export type TeamDocument = HydratedDocument<Team>;
  4. @Schema()
  5. export class Team {
  6. @Prop()
  7. username: string;
  8. @Prop()
  9. password: string;
  10. }
  11. export const TeamSchema = SchemaFactory.createForClass(Team);


我将团队中的objectId添加到夜晚的方式是:

  1. async addTeamToNight(teamId: string, addTeamToNightDTO: AddTeamToNightDTO) {
  2. const name = addTeamToNightDTO.name;
  3. const password = addTeamToNightDTO.password;
  4. const night = await this.nightModel.findOne({name, password}).exec();
  5. if (night) {
  6. night.teams.push(new Types.ObjectId(teamId));
  7. return await night.save();
  8. } else {
  9. throw new NotFoundException('Name or Password incorrect');
  10. }
  11. }


我做错了什么?

pkmbmrz7

pkmbmrz71#

要填充团队数组,您必须执行以下操作:

  1. const teams = await this.nightModel.find({ _id: nightId })
  2. .populate({ path: "teams", model: Team.name })

字符串
要将新的teamId添加到night集合中,请执行以下操作:

  1. const night = await this.nightModel.findOneAndUpdate({ _id: nightId },{$push:{teams: new Types.ObjectId(teamId) }})


good luck ;)

相关问题