Home > Software design >  how can i multiple populate in same query using mongoose
how can i multiple populate in same query using mongoose

Time:03-30

I need multiple populate in the same query.

User.find({$or:[{region: "NA"},{sector:"Some Sector"}]}, function(err, user) { if (err) { res.send(err); } console.log(user); res.json(user);});

CodePudding user response:

Post.model.js

const PostSchema = new Schema(
  {
    postTitle: {
      type: String,
      required: true,
    },
    users: [{ type: Schema.Types.ObjectId, ref: 'User' }],
  },
  { timestamps: true },
);

Query like this

const posts = await Post.find()
      .populate('users', '_id firstName lastName email');
  • Related