Enable to set ObjectId in nested array in nest.js - mongodb

I have a module of question, and I want a structure something like
[ { question: 'REFERENCE ID', answer: { start_value: '1', end_value: '2' } } ],
when I try to save it, it save question id as string but I want to store questionId as ObjectId so I can populate it in other request.
while and the complete response I want is:
{
"answer_by": ObjectId(''), // string, should be user reference ID for populating user
"answers": [
{
"question": ObjectId(''), // string, should be user reference ID for populating question
"answer": {
"start_value": "val1",
"end_value": "val2"
}
}
]
}
here is my schema respectively!
import * as mongoose from "mongoose";
import { Person } from "./person.schema";
import { PostMeetingChecklist, PostMeetingChecklistDocument } from "./post-meeting-checklist.schema";
import { Model, Schema as MongooseSchema } from "mongoose";
import { Resource } from "./resource.schema";
#Schema()
export class Answer {
#Prop()
start_value: string;
#Prop()
end_value: string;
}
#Schema()
export class Item {
#Prop({ type: mongoose.Schema.Types.ObjectId, ref: Question.name})
question: string;
#Prop()
answer: Answer;
}
export type QuestionDocument = Question & mongoose.Document;
#Schema({ timestamps: true })
export class Question {
#Prop({ type: mongoose.Schema.Types.ObjectId, ref: User.name })
answer_by: string;
#Prop()
answers: [Item];
}
export const QuestionSchema = SchemaFactory.createForClass(Question);
in mongoDb,it takes answer_by as ObjectId but question as string, how to fixed that?
Further, I also want to populate this like:
{
"answer_by": User Details,
"answers": [
{
"question": Question Details,
"answer": {
"start_value": "5",
"end_value": "10"
}
}
],
"_id": "63f3146f9f84a58be0b32ad8",
"createdAt": "2023-02-20T06:34:23.300Z",
"updatedAt": "2023-02-20T06:34:23.300Z",
"__v": 0
}
Have tried
#Prop()
answers: [{
question:{
type:mongoose.Schema.Types.ObjectId,
ref:"PostMeetingChecklist"
},
answer: Answer
}];
but in vain.

Related

Add a new field to the Typegoose subdocument

How do I include an extra field in the User class subdocument?
class Car {
#prop()
public model?: string;
}
class User {
#prop()
public name?: string;
#prop({ required: true })
public age!: number;
#prop({ ref: () => Car })
public cars?: Ref<Car>[];
}
Populated Car Collection:
Car A
{
"_id": "1"
"model": "Ferrari"
}
Car B
{
"_id": "2"
"model": "Tesla"
}
User collection populated according to the default class:
User
{
"_id": "1",
"name": "Jonh Doe",
"age": 25,
"cars": [
{ "_id": "1" },
{ "_id": "2" }
]
}
I need to include a "status" field in the cars array of the User collection, as shown below:
status is to identify whether the car model is active for the user
User
{
"_id": "1",
"name": "Jonh Doe",
"age": 25,
"cars": [
{ "_id": "1", "status": false },
{ "_id": "2", "status": true }
]
}
Hasezoey helped me with the github issue.
Here's the link and his answer.
https://github.com/typegoose/typegoose/discussions/726
You have multiple options:
make the current reference array a subdocumentarray (see example 1)
include the status property on the car itself
have a separate class that has a unique compound index on the user-car references and has the properties you want and have such a reference field in the user (either through virtual populate or with a explicit ref array) (see example 2)
Example 1:
class Car {
#prop()
public model?: string;
}
class UserCarSub {
#prop({ ref: () => Car })
public car: Ref<Car>;
#prop()
public extra_properties: string;
}
class User {
#prop()
public name?: string;
#prop({ required: true })
public age!: number;
#prop()
public cars?: UserCarSub[];
}
Example 2:
// the first example does not use virtual populate
class Car {
#prop()
public model?: string;
}
class UserCarProperties {
#prop({ ref: () => Car })
public car: Ref<Car>;
#prop({ ref: () => User })
public user: Ref<User>;
#prop()
public extra_properties: string;
}
class User {
#prop()
public name?: string;
#prop({ required: true })
public age!: number;
#prop({ ref: () => Car })
public cars?: Ref<Car>[];
#prop({ ref: () => UserCarProperties })
public car_properties: Ref<UserCarProperties>[];
}
// the following uses virtual populate
class User {
#prop()
public name?: string;
#prop({ required: true })
public age!: number;
#prop({ ref: () => Car })
public cars?: Ref<Car>[];
#prop({ ref: () => UserCarProperties, foreignField: 'user', localField: '_id' })
public car_properties: Ref<UserCarProperties>[]; // this property does not exist in the database, but can still populate
}
Virtual Populate
Alternatively you could also in Example 2 merge the cars references into the UserCarProperties and use that to find cars, that is fully up to how you want it
Personally i would suggest Example 2 with virtual populate (merged) if the array is of unknown length or could grow to unknown length

How to accept multiple objects into an array NestJS

I have a feedbackQuestion schema which takes (title: string, subtitle: string, types: enum, values: enum)
import { Prop, Schema, SchemaFactory } from '#nestjs/mongoose'
import { Document } from 'mongoose'
import { Types, Value } from 'src/common/enum/types.enum'
export type FeedbackQuestionDocument = FeedbackQuestion & Document
#Schema({ timestamps: true, id: true })
export class FeedbackQuestion {
#Prop()
title: string
#Prop()
subtitle: string
#Prop()
types: Types
#Prop()
value: Value
}
export const FeedbackQuestionSchema =
SchemaFactory.createForClass(FeedbackQuestion)
The feedbackQuestion schema serves as a subdocument in my feedback schema for the key question
import { Prop, Schema, SchemaFactory } from '#nestjs/mongoose'
import mongoose, { Document, ObjectId } from 'mongoose'
import { User } from './user.schema'
import { Transform, Type } from 'class-transformer'
import { FeedbackQuestion } from './feedback-question.schema'
import { distributionChannels } from 'src/common/enum/distributionChannels.enum'
export type FeedbackDocument = Feedback & Document
#Schema({ timestamps: true, id: true })
export class Feedback {
#Transform(({ value }) => value.toString())
_id: ObjectId
#Prop()
label: string
#Prop({ default: false })
status: boolean
#Prop()
question: [FeedbackQuestion]
#Prop()
comment: string
#Prop()
thankYouMessage: string
#Prop()
distributionChannels: distributionChannels
#Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
#Type(() => User)
user: User
}
export const FeedbackSchema = SchemaFactory.createForClass(Feedback)
when creating my create-feedbackDto, I assigned question to be an array of type feedbackQuestion
import { Type } from 'class-transformer'
import { FeedbackQuestion } from '../../schemas/feedback-question.schema'
import { IsArray, IsEnum, IsNotEmpty, ValidateNested } from 'class-validator'
import { Types, Value } from '../enum/types.enum'
import { distributionChannels } from '../enum/distributionChannels.enum'
export class CreateFeedbackDto {
#IsNotEmpty()
label: string
status: boolean
#IsArray()
#ValidateNested({ each: true })
#Type(() => FeedbackQuestion)
question: FeedbackQuestion[]
#IsNotEmpty()
comment: string
#IsNotEmpty()
thankYouMessage: string
#IsNotEmpty()
title: string
#IsNotEmpty()
subtitle: string
#IsEnum(Types)
#IsNotEmpty()
types: Types
#IsEnum(Value)
#IsNotEmpty()
value: Value
#IsEnum(distributionChannels)
#IsNotEmpty()
distributionChannels: distributionChannels
}
In my feedback services, I want to work on questions such that I can pass in multiple objects of feedbackQuestion into the question array when creating a feedback. Please How can I do that?
The current code only takes one FeedbackQuestion object in the array
import { Injectable } from '#nestjs/common'
import { InjectModel } from '#nestjs/mongoose'
import { Model } from 'mongoose'
import { Feedback, FeedbackDocument } from '../../schemas/feedback.schema'
import {
FeedbackQuestion,
FeedbackQuestionDocument,
} from '../../schemas/feedback-question.schema'
import { IServiceResponse } from '../../common/interfaces/service.interface'
import { CreateFeedbackDto } from 'src/common/dto/create-feedback.dto'
#Inject#5406able()
export class FeedbackService {
constructor(
#Inject#5406Model(Feedback.name)
private feedbackDocumentModel: Model<FeedbackDocument>,
#Inject#5406Model(FeedbackQuestion.name)
private feedbackQuestionDocumentModel: Model<FeedbackQuestionDocument>,
) {}
async createFeedback(payload: CreateFeedbackDto): Promise<IServiceResponse> {
const feedbackQuestion = await this.feedbackQuestionDocumentModel.create({
title: payload.title,
subtitle: payload.subtitle,
type: payload.types,
value: payload.value,
})
const feedback = await this.feedbackDocumentModel.create({
label: payload.label,
status: payload.status,
question: [feedbackQuestion],
comment: payload.comment,
thankYouMesage: payload.thankYouMessage,
distributionChannels: payload.distributionChannels,
})
// feedback.question.push(feedbackQuestion)
await feedback.save()
return {
data: {
user: feedback,
},
}
}
}
This is the current response I get
"label": "Yearly Feedback",
"status": false,
"question": [
{
"title": "Yearly Feedback",
"subtitle": "Rating the Yearly performance of the organization",
"value": 1,
"_id": "627fa9b915d31bbbc0fe6908",
"createdAt": "2022-05-14T13:08:09.180Z",
"updatedAt": "2022-05-14T13:08:09.180Z",
"__v": 0
}
],
Thanks

Mongoose Populate() replaces array with object

My schema looks like this
Schema()
export class User {
#Prop({ required: true })
name: string;
#Prop({ required: true })
email: string;
#Prop({ type: mongoose.Types.ObjectId, ref: 'Course' })
courses: Course[];
}
#Schema()
export class Course {
#Prop()
name: string;
#Prop([{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }])
participants: User[];
#Prop([{ type: mongoose.Schema.Types.ObjectId, ref: 'Lesson' }])
lessons: Lesson[];
}
what i'm trying to achieve is to get an array of courses that a specific user is signed up for. The way Im trying to do this looks like this.
async findUserCoursesByUserEmail(email) {
const user = await this.userModel
.findOne({ email })
.populate('courses')
.exec();
return user.courses;
}
But in response i get an object like this, but it should be an array.
{
"lessons": [],
"participants": [
"6157a5ba06afba420464fb65"
],
"_id": "6157a5ac06afba420464fb61",
"name": "Matematyka Rozszrzona",
"__v": 1
}
The user object looks like this.
{
"_id": "6157a5ba06afba420464fb65",
"name": "Jacob",
"email": "test#test.pl",
"courses": [
"6157a5ac06afba420464fb61",
"6158d64891be3f50f85bef0a"
],
"__v": 2
}
Any help would be highly appreciated cause i'm stuck for quite some time with this one.
as per nestjs/mongodb docs change the:
#Prop({ type: mongoose.Types.ObjectId, ref: 'Course' })
courses: Course[];
on your UserSchema to:
#Prop({ type: [{mongoose.Types.ObjectId, ref: 'Course'}] })
courses: Course[];

Return the actual document instead of ObjectId

So, I have a model called Drivers that receive a field called "user", which references a document from another model, like this:
const DriversSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
},
{
timestamps: true,
}
);
// ...
Querying the collection works as expected, here's an example:
Drivers.find({});
// returns ->
[
{
"name": "John",
"user": "5e43f8ad2fbb1d0035d5a154",
}
]
Is there a way to return the actual document represented by the 'user' field?
Thanks!

Typegoose - ObjectId that references a Sub-Document

I'm trying to pull a ref from a Subdocument. The Documents looks like this.
Sample Categories Doc:
{
"_id": 12345
"keyword": "smart phones",
"subCategories": [
{
"_id": 6789,
"keyword": "apple"
},
{
"_id": 101123,
"keyword": "samsung"
}
]
}
Sample Dictionary Doc:
{
"_id": 12345
"keyword": "iPhone",
"category": 12345,
"subCategory": 6789
}
Here's what I've tried to do on Typegoose model definition:
For Dictionary (notice the subCategory prop, I'm not sure if that's the right way to reference a subdocument):
export class Dictionary extends Typegoose {
_id!: Schema.Types.ObjectId;
#prop({
default: 1
})
type!: IDictionaryInput['type'];
#prop()
mainKeyword!: string;
#arrayProp({ items: Synonym })
synonyms!: Synonym[];
#prop({ ref: Category })
category!: Ref<Category>;
#prop({ ref: CategoryModel.subCategories })
subCategory!: Ref<Subcategory>;
#prop({
default: true
})
status!: IDictionaryInput['status'];
}
For Categories:
export class Category extends Typegoose {
_id!: Schema.Types.ObjectId;
#prop()
keyword!: ICategoryInput['keyword'];
#prop({
default: 1
})
type!: ICategoryInput['type'];
#arrayProp({ items: Subcategory })
subCategories!: Subcategory[];
#prop({
default: true
})
status!: ICategoryInput['status'];
#prop()
insertTimestamp!: ICategoryInput['insertTimestamp'];
}
Then I tried to populate the references by doing:
DictionaryModel.findOne({ _id: id })
.populate({
path: 'category',
model: CategoryModel
})
.populate({
path: 'subCategory',
model: CategoryModel.subCategories
});
I can successfully populate the ref from category but not on subCategory.