Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

420
Views
How to query and populate subdocuments only inside a document?

I have a collection as follows, enter image description here

i want to query and populate only reviews which has the fromUser userID '233232'
Right now my helper class is as follows,

 queryaggregateWithProject: function (model, matchedCond, prjCond) {
            return new Promise(function (resolve, reject) {
                model.aggregate([
                    {$match: matchedCond},
                    {$project: prjCond}
                ], function (error, data) {
                    if (error)
                        reject(error);
                    resolve(data);
                });
            })
        }

what should i pass for matchedCond and projCond?

Schema

module.exports = function () {
    var model = null;
    var modelName = '_purchase';
    try {
        model = dbService.getModel(modelName);
    } catch (error) {     

        var userSchema = dbService.createEntityDef({
            userId: {
                type: String
            },            
            urlPicture: {
                type: String
            }
        });
        var reviewSchema = dbService.createEntityDef({
            fromUser: userSchema,
            toUser: userSchema,
            rating: {
                type: Number
            },
            comment: {
                type: String
            }


        })


        var purchaseSchema = {
            _id: {
                type: dbService.getPrimaryKeyType(),
                index: true
            },
            purchaseId: {
                type: String
            },          
            reviews: [reviewSchema]

        };
        model = dbService.createModel(modelName, purchaseSchema, {
            name: 'text'
        }, modelName);
    }

    return model;
};
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your matchedCond

{ fromUser.userid : "youruserid" }

Your projCond

{ document: "$$ROOT" }

will return the objects with all properties.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!