Careeroza
Real World Example
Activity Feed in a Social Commerce App
Social commerce apps (Meesho, ShareChat shopping, Instagram-style feeds) store flexible "activity" documents: different types have different fields. MongoDB discriminators model this cleanly.
src/feed/schemas/activity.schema.ts
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
@Schema({ discriminatorKey: 'type', timestamps: true })
export class Activity {
@Prop({ required: true, index: true }) userId: number;
@Prop({ required: true, enum: ['ORDER_PLACED', 'REVIEW_ADDED', 'WISHLISTED'] }) type: string;
}
export const ActivitySchema = SchemaFactory.createForClass(Activity);
@Schema()
export class OrderPlaced { @Prop() orderId: string; @Prop() itemCount: number; }
export const OrderPlacedSchema = SchemaFactory.createForClass(OrderPlaced);
@Schema()
export class ReviewAdded { @Prop() productId: number; @Prop() rating: number; }
export const ReviewAddedSchema = SchemaFactory.createForClass(ReviewAdded);
// feed.module.ts
// MongooseModule.forFeature([{
// name: Activity.name, schema: ActivitySchema,
// discriminators: [
// { name: 'ORDER_PLACED', schema: OrderPlacedSchema },
// { name: 'REVIEW_ADDED', schema: ReviewAddedSchema },
// ],
// }])Example
curl "http://localhost:3000/feed?userId=12&limit=3"Example output
[{"type":"REVIEW_ADDED","userId":12,"productId":7,"rating":4,"createdAt":"2026-01-15T10:00:00.000Z"},
{"type":"ORDER_PLACED","userId":12,"orderId":"ORD-7781","itemCount":2,"createdAt":"2026-01-14T18:22:10.000Z"},
{"type":"WISHLISTED","userId":12,"createdAt":"2026-01-13T09:05:44.000Z"}]