0

I want to find only 20 Parent records in LIFO order if it has more than 5 children. Children is a different table. Highly appreciate your help!

Parent table name - Content

import { model, Schema, Model, Document } from "mongoose";

const ContentSchema: Schema = new Schema({
  userId: String,
  contentType: String,
},
  { timestamps: { createdAt: 'createdOn' } }
);

export const Content = model("Content", ContentSchema);

Child table name - Transaction

import { model, Schema, Model, Document } from "mongoose";

const TransactionSchema: Schema = new Schema({
  contentId: { type: Schema.Types.ObjectId, ref: 'Content' }
},
  { timestamps: { createdAt: 'createdOn' } }
);

export const Transaction = model("Transaction", TransactionSchema);
Bibek Das
  • 35
  • 10
  • This sounds like a fairly simple task for aggregation. Have you tried using $group, $sort, and $limit? – Joe Dec 22 '22 at 10:41

0 Answers0