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

520
Views
How to search in multiple collections in MongoDB using Mongoose

I have two collections User and UserType :-

var User = new mongoose.Schema({ 

     username: {
      type: String,
      required: true,
   },

   userType: {
      type: ObjectId,
      ref: "UserType",
      required: true,
   },
});

 var UserTypeSchema = new mongoose.Schema(
   {
      type: String,
      type_code: Number,
      type_description: String,
   },
   { timestamps: true }
);

I want to search user based on username and typecode which is in UserType Collection. I tried this code: -

    User.findOne({
  username: mobileNumber,
  userType: { type_code: userTypeCode },
})
   .populate("userType");

please correct this query.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

you must to filter out populate results, with match option In your case answer would be::

User.findOne({
  username: mobileNumber,
}).populate({
  path: "userType",
  match: { type_code: userTypeCode },
});

you can check the documentation

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!